Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-26-2012, 10:08 AM   #1
LQ Newbie
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
how to delete lines according to a pattern in awk

I need to delete(or skip) next three lines after a pattern is found in some line of the file.
Does anybpdy have an idea how to do that in awk?

Old 01-26-2012, 10:35 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Use the getline function three times
Old 01-26-2012, 12:54 PM   #3
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427

If you don't mind using sed,
$ echo -e '1\n2\n3\n4\n5\n6' | sed '/2/{n;N;N;d}'
From `man sed':
n N Read/append the next line of input into the pattern space.
d Delete pattern space. Start next cycle.
If you replace `n' by `N', the line with a pattern will also be removed.
In GNU sed the latter can be done also as follows
$ echo -e '1\n2\n3\n4\n5\n6' | sed '/2/,+3 d'
Old 01-26-2012, 01:40 PM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,629

Rep: Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944
Record NR value plus 3 at point of finding what you want and only print more details when you get to corresponding NR value
Old 01-26-2012, 04:19 PM   #5
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
Human languages are sometimes frustratingly vague. You thought you stated your desire accurately, but there are four solutions with different results that all do what you stated you want.

If you want to keep the line containing the matching pattern, but omit the three next lines, use
awk '          { print }
     /pattern/ { getline ; getline ; getline ; next }
    ' input-file > output-file
which uses getline three times like Catkin said. The three lines are completely ignored. In particular, the script does not check if the pattern matches in the deleted lines. Therefore, if you have the pattern on four consecutive lines, only three lines are omitted.

If you want to the three lines including the one containing the pattern, use
awk '/pattern/ { getline ; getline ; getline ; next }
               { print }
    ' input-file > output-file
In this case, if you have three consecutive lines containing the pattern, they are all omitted, but the immediately following line not containing the pattern will be included.

If you want to check if the omitted lines trigger further omissions, use Grail's approach:
awk '/pattern/ { nNR = NR + 3 }
            NR >= nNR { print }
    ' input-file > output-file
If you want the omission to omit the three lines that follow it, but not the triggering line itself (unless that line is omitted due to a prior match), use
awk '       NR >= nNR { print }
     /pattern/ { nNR = NR + 4 }
    ' input-file > output-file
I recommend you test the above with different types of input, not just the one you expect to receive, and pick the one that matches your needs.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting lines from a file with specific pattern using AWK gandhigaurav1986 Programming 12 06-08-2010 03:08 AM
How to use sed to delete all lines before the first match of a pattern? C_Blade Linux - Newbie 9 05-01-2010 05:18 AM
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 02:11 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 12:08 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration