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 ...
-
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!
-
Should be something like:
Code:
awk '/aaa/ {if(s){print s}s="";f=1}f{s=s $0}' file
-
Many thanks! Just what I needed

Originally Posted by
Franklin52
Should be something like:
Code:
awk '/aaa/ {if(s){print s}s="";f=1}f{s=s $0}' file
-
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
?

Originally Posted by
adriana
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!
-

Originally Posted by
adriana
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
-

Originally Posted by
Franklin52
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
?
-

Originally Posted by
adriana
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
-

Originally Posted by
Franklin52
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
-
Forum Rules