dimanche 7 juin 2020

Search for 3+ consecutive lines in a file matching criteria

I would like to search a file for every instance of >=3 consecutive rows which match a given criteria. I would like to print out columns #1 and #2 from the first matched row, and column #3 from the last matched row for each block of consecutive matches.

For example, Let's say I'm looking for multiple rows with > 500 in column 4. Here's what I am looking for:

Example File

Sc000000    2400    3600    602
Sc000000    3600    4800    835
Sc000000    4800    6000    718
Sc000000    6000    7200    416
Sc000000    7200    8400    602
Sc000000    8400    9600    615
Sc000000    9600    10800   125
Sc000000    10800   12000   875
Sc000000    12000   13200   753
Sc000000    13200   14400   567

Results File

Sc000000    2400    6000
Sc000000    10800   14400

I have tried awk '/{$4 >= 500}/{f++; if (f>=3) print; last=$0; next} {f=0}' file.txt but besides my syntax being wrong (I am new to awk), it would print every line instead of the summarised output I am looking for. Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire