How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi ,

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

Thanks,
Venkat Vadlamudi.

Try

tac file | awk '/Field 039 00/ {L=NR+3} NR>L' | tac

Here is another solution:

#! /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.

Another awk solution by reading input file twice:

awk '
        NR == FNR {
                if ( $0 ~ /Field 039 00/ )
                        A[NR]
                next
        }
        !((FNR-3) in A) && !((FNR-2) in A) && !((FNR-1) in A) && !(FNR in A)
' file file

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.

awk 'BEGIN { a=5; print "ZERO: " a[0]; print "EMPTY: " a[""]}'
ZERO: 
EMPTY: 5

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).

Regards,
Alister

1 Like

If it is always sets of 4 lines, then this might also do:

sed '$!N;$!N;$!N;/\nField 039 00$/d'

Hi.

A grep relative, cgrep, can do this:

#!/usr/bin/env bash

# @(#) s1	Demonstrate match+previous 3, then invert to delete; cgrep.
# See: http://sourceforge.net/projects/cgrep/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C column cgrep

FILE=${1-data1}

pl " Input data file $FILE (columnized):"
column $FILE

pl " Results:"
cgrep -D -V -3 fig $FILE

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
column - ( /usr/bin/column, 2007-11-20 )
cgrep ATT cgrep 8.15

-----
 Input data file data1 (columnized):
apple		date		kiwi		nectarine	rhubarb
banana		fig		lemon		orange
cherry		grape		mango		peach

-----
 Results:
apple
grape
kiwi
lemon
mango
nectarine
orange
peach
rhubarb

See sourceforge page for details. It will need to be compiled, but I had no trouble with that.

Best wishes ... cheers, drl

Its working Fine. awrsome..Thank you.. How can i search other than "Field 039 00" like "Field 039 47", "Field 039 91" ,etc ?

---------- Post updated at 04:25 AM ---------- Previous update was at 04:25 AM ----------

Tac command is not workig.