i have a file with data as below.This is same file. But actual file contains to many rows.
i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ?
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
Field 039 00
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
Field 039 91
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
Field 039 00
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
Field 039 91
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
Field 039 00
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
Field 039 00
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
Field 039 00
Field 004 000000000100
Field 007 1125112233
Field 102 7000005253
#! /bin/bash
awk ' BEGIN {i=0;}
/Field 039 00/ { count = 0 ; next}
{
Store=$0;
i = (i+1) % 4;
if (count < 3)
count++;
else
print Store;
}
END {
while (count--) {
i = (i+1) % 4;
print Store;
}}' data
exit 0
It did not work until I put in the BEGIN block. I have to say I don't understand why. I expected i to just pop into existence with a value of 0. Maybe that doesn't work with subscripts.
AWK's arrays are associative and its subscripts are strings. i does pop into existence, but, in that string context, its undefined value is converted to an empty string.
An alternative fix would be to cast i to a numeric, Store[i+0]=$0 , before AWK automatically casts it back to a string (technically there is no cast, as the variable can have both string and numeric values at the same time and the implementation simply chooses which to use depending on context).