Awk/sed to delete block of lines w/ condition

Sony Bdp-s570 blu-ray disc player
August 19, 2010 at 19:23:26
Specs: Linux
I am trying to write a sed or awk script that will delete a block of text from a large file if somewhere in that block I find a numeric value greater than a constant.

input file could look like this
header_1;
header_2;
header_n {
some stuff
}

data_record () {
some more stuff;
even more stuff;
value : 0.0012234 ;
}

data_record () {
some more stuff;
even more stuff;
value : 120.123 ;
}

more stuff
<EOF>

I want to delete blocks of text between data_record () {
and the closing brace --ONLY IF the numeric value statement in the "value : xxxx" line is greater than a constant, say 17

I got sed "/data_record /,/\}/d" <input_file
figured out
and an equivalent in awk
but I can't figure out how to dellete only if it finds that value line and then only if the numberic after the colon is greater than my desired cutoff value.

any takers?


See More: Awk/sed to delete block of lines w/ condition

Report •

#1
August 20, 2010 at 13:02:21
I think I figured it out.

this awk script removes specific records whenever the numeric value is larger than 17
#find keyword
/data_record \(/ {
f[nf=1] = $0 # store in array
keepblock = 1 # keep this record
while (getline && $0 !~ /\}/) { # while loop accumulates the next lines until an end brace } is found
f[++nf] = $0 # store line into next element of array
if ($1=="value" && $3>=17) { # check if value is too high
keepblock = 0 # if it is, set this record to not keep
}
} # finished while loop
# now process the data in f[1]...f[nf]
f[++nf] = $0 # add ending line in block to array
if (keepblock == 1) { # print out entire array if this record is to be kept
for ( i = 1; i <= nf; i++ ) { print f[i] }
}
getline # get next line
if ($0) {print $0} # if next line is not blank then print it
}
# if keyword is not found then just print the line
!/data_record \(/ {print $0}


I had trouble with the last brace of the record and whether the next line was a blank or not. This caused the getline; if($0) {print $0} statement. I wasn't really sure why I had to add that.


Report •

#2
August 23, 2010 at 00:51:27
awk -vRS="" -vFS=";" 'f{f=0}{for(i=1;i<=NF;i++)if($i~/value/) {split($i,a,":") ; if (a[2]>17) {f=1}  } }f==0' file

GNU win32 packages | Gawk


Report •
Related Solutions


Ask Question