1

I want to extract the lines between PAT 1 to PAT 2 including the pattern lines depending only on the first column, the patterns are only existing for one time in the first column but they may exist for more than one time in the other columns. example for my input file

0.000 0.8928 0.893
0.500 PAT1 0.902
PAT1 0.8974 0.9116
1.500 0.8986 0.9199
2.000 0.8989 0.9258
2.500 PAT1  0.9285
3.000 0.8978 0.9272
3.500 0.8959 0.9219
PAT2  0.8936 0.9128
4.500 0.8912 0.9009
5.000 0.8889 PAT2
5.500 0.887 0.8718
6.000 PAT2 0.8565
6.500 0.8858 0.8414
7.000 0.8871 0.8271

my output should be like this

PAT1 0.8974 0.9116
    1.500 0.8986 0.9199
    2.000 0.8989 0.9258
    2.500 PAT1  0.9285
    3.000 0.8978 0.9272
    3.500 0.8959 0.9219
    PAT2  0.8936 0.9128

my trail is to use awk as following

awk '/PAT1/,/PAT2/' input

but this is independent on the column

4

Change your awk code like this:

awk '$1 ~ /PAT1/,$1 ~ /PAT2/' input

This way, you match against column 1, not against the whole line.

1

With sed:

$ sed -n '/^PAT1/,/^PAT2/p' input
PAT1 0.8974 0.9116 
1.500 0.8986 0.9199
2.000 0.8989 0.9258
2.500 PAT1  0.9285 
3.000 0.8978 0.9272
3.500 0.8959 0.9219
PAT2  0.8936 0.9128

The sed script will only print (p) lines from the input that lies in the range /^PAT1/ to /^PAT2/ inclusively (default output of every line is turned off with -n).

The anchors (^) are necessary to only match the strings at the start of the line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.