tags:

views:

34

answers:

2

Say you have records in a text file which look like this:

header
data1
data2
data3

I would like to delete the whole record if data1 is a given string. I presume this needs awk which I do not know.

+3  A: 

Awk can handle these multiline records by setting the record separator to the empty string:

BEGIN { RS = ""; ORS = "\n\n" }
$2 == "some string" { next } # skip this record
{ print } # print (non-skipped) record

You can save this in a file (eg remove.awk) and execute it with awk -f remove.awk data.txt > newdata.txt

This assumes your data is of the format:

header
data
....

header
data
...

If there are no blank lines between the records, you need to manually split the records (this is with 4 lines per record):

{ a[++i] = $0 }
i == 2 && a[i] == "some string" { skip = 1 }
i == 4 && ! skip { for (i = 1; i <= 4; i++) print a[i] }
i == 4 { skip = 0; i = 0 }
schot
A: 

without knowing what output you desired and insufficient sample input.

awk 'BEGIN{RS=""}!/data1/' file
ghostdog74