Find the answer to your Linux question:
Results 1 to 3 of 3
Hello all, I have a question regarding a bash script: how can I read a file and put in a array all the lines that are after each blank lines. ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    4

    Question bash script to select lines

    Hello all,

    I have a question regarding a bash script:
    how can I read a file and put in a array all the lines that are after each blank lines.

    ex:

    aaa
    111

    bbb
    222
    333

    ccc
    555

    put in the array only aaa bbb ccc

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    if the input file is called "w" then this seems to work OK

    awk 'BEGIN {ORS=" "};/^$/{getline a; print a}' <w

    the regular expression "^$" matches empty lines;
    with the Output Record Separator reset to blank
    (from newline) we get the next lines concatenated
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Jul 2009
    Posts
    4
    Thanks a lot!


    Quote Originally Posted by tpl View Post
    welcome to the forum

    if the input file is called "w" then this seems to work OK

    awk 'BEGIN {ORS=" "};/^$/{getline a; print a}' <w

    the regular expression "^$" matches empty lines;
    with the Output Record Separator reset to blank
    (from newline) we get the next lines concatenated

Posting Permissions

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