Execution problem with print out record that follow specific pattern

Hi,

Do anybody know how to print out only those record that column 1 is "a" , then followed by "b"?
Input file :

a       comp92  2404242 2405172
b       comp92  2405303 2406323
b       comp92  2408786 2410278
a       comp92  2410271 2410337
a       comp87  1239833 1240418
b       comp87  1240496 1240829
b       comp87  1241046 1243683
a       comp87  1243841 1244840
a       comp87  1243841 1244840
b       comp87  1244968 1246522

Output file :

a       comp92  2404242 2405172
b       comp92  2405303 2406323
a       comp87  1239833 1240418
b       comp87  1240496 1240829
a       comp87  1243841 1244840
b       comp87  1244968 1246522

Thanks.

1 Like

Hi guy,
Try this:

sed -n '/^a[[:blank:]]\{1,\}/{N;/\nb[[:blank:]]\{1,\}/p;D}'

cheers.

2 Likes

Awk approach with fixed strings:

awk '$1=="b" && k=="a" {print p ORS $0}{p=$0; k=$1}' file

--
@lucas: nice sed. \{1,\} can be left out, no?

*edit* it could also be done like this.

sed 'N;/^a[[:blank:]].*\nb[[:blank:]]/!D' file
2 Likes

Hi Scrutinizer,
Thanks Scurtinizer, thanks for your advice.
\{1,\} is not nessesary, a [[:blank:]] is totally enough, the sed code is better after you edited it.

1 Like