Find the answer to your Linux question:
Results 1 to 8 of 8
Hi, Thanks for reading this, and maybe helping. I want to solve the following problem. I have the file: xxx aaa xxx xxx xxx aaa xxx aaa yyy zzz and ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5

    help with using awk or grep

    Hi,

    Thanks for reading this, and maybe helping.

    I want to solve the following problem.
    I have the file:
    xxx
    aaa
    xxx
    xxx
    xxx
    aaa
    xxx
    aaa
    yyy
    zzz

    and what to get the lines in between "aaa"s on the same line, smth like:
    aaaxxxxxxxxx
    aaaxxx

    note that I don't print after the last "aaa"

    Any ideas are appreciate!

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Should be something like:
    Code:
    awk '/aaa/ {if(s){print s}s="";f=1}f{s=s $0}' file

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    Many thanks! Just what I needed

    Quote Originally Posted by Franklin52 View Post
    Should be something like:
    Code:
    awk '/aaa/ {if(s){print s}s="";f=1}f{s=s $0}' file

  4. #4
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    One more small modification: on the line where I search the pattern I want to print only the first field

    So, if my input fle lokks smth like
    xxx
    aaa ggg
    xxx
    xxx
    xxx
    aaa bbb
    xxx
    aaa ccc
    yyy
    zzz

    I what to get the same as before:
    aaaxxxxxxxxx
    aaaxxx

    ?


    Quote Originally Posted by adriana View Post
    Hi,

    Thanks for reading this, and maybe helping.

    I want to solve the following problem.
    I have the file:
    xxx
    aaa
    xxx
    xxx
    xxx
    aaa
    xxx
    aaa
    yyy
    zzz

    and what to get the lines in between "aaa"s on the same line, smth like:
    aaaxxxxxxxxx
    aaaxxx

    note that I don't print after the last "aaa"




    Any ideas are appreciate!

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by adriana View Post
    One more small modification: on the line where I search the pattern I want to print only the first field

    So, if my input fle lokks smth like
    xxx
    aaa ggg
    xxx
    xxx
    xxx
    aaa bbb
    xxx
    aaa ccc
    yyy
    zzz

    I what to get the same as before:
    aaaxxxxxxxxx
    aaaxxx

    ?
    Judt replace $0 with $1:

    Code:
    awk '/aaa/ {if(s){print s}s="";f=1}f{s=s $1}' file

  6. #6
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    Quote Originally Posted by Franklin52 View Post
    Judt replace $0 with $1:

    Code:
    awk '/aaa/ {if(s){print s}s="";f=1}f{s=s $1}' file


    One more modification:

    I want to have a space between the first field in the pattern and all the others
    The result should look smth like:

    aaa xxxxxxxxx
    aaa xxx

    ?

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by adriana View Post
    One more modification:

    I want to have a space between the first field in the pattern and all the others
    The result should look smth like:

    aaa xxxxxxxxx
    aaa xxx

    ?
    With a slightly modification:

    Code:
    awk '/aaa/ {if(s){print s}s="";f=1}f{s=s?s $1:$1 " "}' file

  8. #8
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    Quote Originally Posted by Franklin52 View Post
    With a slightly modification:

    Code:
    awk '/aaa/ {if(s){print s}s="";f=1}f{s=s?s $1:$1 " "}' file

    Great! Many thanks!

Posting Permissions

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