LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 01-26-2012, 09:08 AM   #1
Massimo34
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?

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

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Use the getline function three times
 
Old 01-26-2012, 11:54 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

If you don't mind using sed,
Code:
$ echo -e '1\n2\n3\n4\n5\n6' | sed '/2/{n;N;N;d}'
1
2
6
From `man sed':
Quote:
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
Code:
$ echo -e '1\n2\n3\n4\n5\n6' | sed '/2/,+3 d'
1
6
 
Old 01-26-2012, 12:40 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 03:19 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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
Code:
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
Code:
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:
Code:
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
Code:
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.
 
  


Reply



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 02: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 04:18 AM
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 01:11 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 PM.

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