LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 11-11-2008, 01:35 PM   #1
Passions
LQ Newbie
 
Registered: Mar 2007
Distribution: RHEL 5.1
Posts: 20

Rep: Reputation: 0
Red face Search for some pattern and then delete N records after it?


So I found a great awk line to search for a pattern and then print the N record after it.

iii) Print the N records after some pattern:

Code:
awk 'c&&!--c;/pattern/{c=N}' file

It's really hard to understand though with the c&&c.

How can I instead of printing the Nth record, delete it?



For example, I have a txtfile:

List of Names
Bob
Janet
Harry

List of Groups
Family
Party
Coworkers


If I do awk 'c&&!--c;/List of/{c=2}' txtfile
It will print:

Janet
Party

Instead of print, how can I delete them? I know with sed I can do

Code:
sed -n '/List of/{n;n;d;}' file
But the number of n's needs to be a variable as I have numerous records.

Thanks!!!

Last edited by Passions; 11-11-2008 at 03:35 PM. Reason: Fixed error in coding. sorry.
 
Old 11-11-2008, 02:06 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Quote:
Originally Posted by Passions View Post
...If I do awk 'c&&c--;/List of/{c=2}' txtfile
It will print:

Janet
Party
...

Not on my system:
Code:
jan@jack:~/tmp/awk_quest> awk 'c&&c--;/List of/{c=2}' data
Bob
Janet
Family
Party
Try this:
Code:
jan@jack:~/tmp/awk_quest> awk '/List of/{c=NR} NR==c+2 {getline}{print}' data
List of Names
Bob
Harry

List of Groups
Family
Coworkers
Jan

EDIT: To put the number into a variable:
Code:
jan@jack:~/tmp/awk_quest> awk -v nr=2 '/List of/{c=NR} NR==c+nr {getline}{print}' data
List of Names
Bob
Harry

List of Groups
Family
Coworkers

Last edited by jan61; 11-11-2008 at 02:09 PM.
 
Old 11-11-2008, 03:49 PM   #3
Passions
LQ Newbie
 
Registered: Mar 2007
Distribution: RHEL 5.1
Posts: 20

Original Poster
Rep: Reputation: 0
Hi,

You're right. I goofed the code on printing the Nth line. It should be:

Code:
awk 'c&&!--c;/pattern/{c=N}' file

I tried your suggestion on deleting the Nth line.

Code:
awk -v nr=3 '/List of/{c=NR} NR==c+nr {getline}{print}' bob
It works well for nr=2, but when I go to the 3rd record it spits out:

List of hosts
Bob
Janet

List of hosts
Family
Party
Coworkers


Coworkers did not get deleted for some reason. I'll try to work on this more, but thanks for your help!
 
Old 11-12-2008, 01:23 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

is the "Coworkers" line the last in the file? Does it end with a newline? I can reproduce the effect, when the line doesn't have a newline.

Jan
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vim search pattern Berticus General 3 08-21-2008 12:13 PM
perl: search for equality pattern kpachopoulos Programming 2 06-02-2007 09:47 PM
pattern search through find command. abhi Linux - Newbie 2 04-11-2005 10:31 AM
delete records on table vickr1z Linux - Newbie 1 11-25-2004 05:27 AM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM


All times are GMT -5. The time now is 06:56 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration