Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    88

    regexp: Return all info between strings when pattern found


    I would like to parse a file I have and return all info between "^#\ file" and "^$" only when I find the pattern "r-x". I have been using regexp, but I am horrible at it. In addition, I don't know how to get positive look-aheads to work on multiple lines. I am sure I am going to learn something new on this one if somebody can help. Thanks in advance

    Sample data:
    Code:
    # file: j_natu/uat2ebs/outgoing/BANK/Ecuador
    group::r-x      #effective:r-x
    mask::r-x
    default:group::rwx      #effective:r-x
    default:mask::r-x
    
    # file: j_natu/uat2ebs/outgoing/BANK/Korea
    group::r-x      #effective:r-x
    mask::r-x
    default:group::r-x      #effective:r-x
    default:mask::r-x
    
    # file: j_natu/uat2ebs/outgoing/Agile
    
    # file: j_natu/uat2ebs/outgoing/Agile/Zip
    
    # file: j_natu/uat2ebs/outgoing/ITALY
    Desired output:
    Code:
    # file: j_natu/uat2ebs/outgoing/BANK/Ecuador
    group::r-x      #effective:r-x
    mask::r-x
    default:group::rwx      #effective:r-x
    default:mask::r-x
    
    # file: j_natu/uat2ebs/outgoing/BANK/Korea
    group::r-x      #effective:r-x
    mask::r-x
    default:group::r-x      #effective:r-x
    default:mask::r-x

  2. #2
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    88
    I found this: https://stackoverflow.com/questions/...ing-sed-or-awk

    This is close. This deletes the blank line whenever the pattern is found, but if I can get it to also delete the line where the pattern is found (only when followed by a blank line), that too would work:

    Code:
    awk -v n=-2 'NR==n+1 && !NF{next} /# file:/ {n=NR}1'

  3. #3
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    88
    -->
    From here: https://unix.stackexchange.com/quest...-using-gnu-sed

    Code:
    sed '/^# file:/{N;/\n$/d}'
    Explanation:
    N is "append a newline and the next input line to the current buffer", so \n in the buffer is the start of an appended line /\n$/d means "if the last appended line in the buffer is empty just drop all of it".

    Still voodoo to me. If someone can clarify the above a little better I would appreciate it.

$spacer_open
$spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •