简体   繁体   中英

How to remove series of lines in a file on hp unix using awk or sed or grep

My report looks like:

Report B566  company name .................................... Page 1

    Name    address   pin   


    John    ny        1111
    Dave    ma        1112
    ....    ....    ....   
    ....    ....    ....   
    ....    ....    ....   
Report B566  company name .................................... Page 2

    Name    address   pin   


    Barry    CA        5111

This way i have around 100 pages, i want to get rid of repetitive headers. i need a command in such way that if "Report B566" is found in the file all of them along with the next 6 lines should be removed and the outfile should contain only the data.

I'm working on HP-UNIX box. (ksh)

Thanks for your help.

You can set a counter and print based on when the counter is true.

$ awk '/Report/{c=6}!(c&&--c)' file
John    ny        1111
Dave    ma        1112
....    ....    ....
....    ....    ....
....    ....    ....
Barry    CA        5111

Sed solution

sed '/Report/,+4d' file

This removes the report line and the four following lines, it's a really simple command.

This might work for you (GNU sed):

sed -r '/Report/{G;/^(\S+\s+\S+\s).*\n\1/!{s/\n.*//;h;b};N;N;N;N;d}' file

Compare Report lines to the previous one and if they are the same remove it and the next five lines. If not the same store it and resume as usual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM