Hi friends,
I am looking for sed command/script that would search for a given fixed pattern on odd lines and then if it matches, prints the matching pattern and the next line. For example, in the example below, i am looking for pattern 0 and 1011 on odd lines.
########## start of example file ############
0
0
1000
x
1001
x
1010
x
1011
xxxxxxxxxxxxxxxx
1100
xxxxxxxxxxxxxxxxxxxx
################# end of example file ##########
The output should be
0
0
1011
xxxxxxxxxxxxxxxx
How to code this in sed? Thanks a lot for your response.
What do you mean by 'odd lines'? One of those 0's comes from an even line(line #2).
1 Like
corona dude
Thanks for your reply. I know what you are talking about. That's why i said, search for pattern on odd lines ONLY. Therefore, you are not searching for 0 on the 2nd line. If first line (odd line) is a 0 or 1011, you will print the next even line.
Does that make sense? Please feel free to ask more questions if it is not clear.
How could you possibly get two zeroes printed then, when that should only cause the one on an even line to be printed?
1 Like
dude
you are printing both the pattern (on the odd line) and the next line (even line). That is why you will get the 2nd zero as a result of pattern match on the previous odd line.
Does that sound good?
Please ask more questions if you feel.
Thanks a lot for your effort.
awk 'NR%2 { L=$0; next }; L=="0" { print L; print }' datafile
1 Like
Try:
sed -n '/^0$/{N;};/^1011$/{N;};/\n/p' infile
1 Like
Hi corona, your solution works for 0. I am wondering how to include other pattern which is 1011.
Scrutinizer, your command is picking up 0 on even lines as well.
Thanks so much for your effort
awk 'NR%2 { L=$0; next }; (L=="0")||(L=="1011") { print L; print }' datafile
1 Like
That's right...
sed -n 'N;/^0\n/p;/^1011\n/p' infile
1 Like
Scrutinizer and Coron688
I am so grateful to both of you.
Hats off to you. I shall join you guys soon to contribute in this wonderful forum.
Keep up the good work guys.
Kind Regards,
kaalia kahn