LOKESH
LOKESH

Reputation: 1301

delete duplicate entry from file with above some line

I have file whose content like below

# Time: 180110 10:48:37
use 65_ebccrmproduction;
SET timestamp=1515561517;
abcdegh
# Time: 180110 10:48:59
SET timestamp=1515561554;
poiuyt
assadd
# Time: 180110 10:49:51
SET timestamp=1515561554;
assddpoiuyt
# Time: 180110 10:49:51
SET timestamp=1515561554;
poiuytassassas

I want to grep and delete block whose content match

block means content between #Time to next #Time

for example I want to match poiuyt

then it will delete below line

# Time: 180110 10:48:59
SET timestamp=1515561554;
poiuyt
assadd
# Time: 180110 10:49:51
SET timestamp=1515561554;
poiuytassassas

I have code sed -n '/poiuyt/{s/.*//;x;d;};x;p;${x;p;}' test.txt | sed '/^$/d' this will delete pattern match and one line above that pattern but my purpose is totally different from this.

Note : This should also delete poiuytassassas

Upvotes: 1

Views: 31

Answers (2)

P....
P....

Reputation: 18371

sed 's/^# Time:.*/\n&/g' inputfile |awk -v RS= '!/poiuyt/{print $0}'

Here, sed is used to properly divide the records, and awk is used to do the filtering.

Upvotes: 2

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Awk solution:

awk '/# Time:/{ 
         if (f && !del){ print lines } 
         lines=$0; del=0; f=1; next 
    }
    f{ 
        lines=lines ORS $0; 
        if (/poiuyt/){ del=1 } 
    }
    END{ if (f && !del) print lines }' file.txt

The output:

# Time: 180110 10:48:37
use 65_ebccrmproduction;
SET timestamp=1515561517;
abcdegh

Upvotes: 2

Related Questions