LinuxQuestions.org
Review your favorite Linux distribution.
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 08-24-2012, 10:31 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
AWK: print to end staring from nth line after matching


Dear Experts,

I have a file like:
Code:
ssssss
oooooo
rrrrrr
aaa
111111
222222
333333
444444
555555
666666
END
What I want is to locate the pattern "aaa" then from there I skip n lines and print remaining lines to the end of the file.

Say, I'd like to skip 3 lines after matching "aaa", then I would expect a result like
Code:
444444
555555
666666
END
I tried the code:
Code:
awk '/aaa/{n=NR} NR > n+3 {print}' INPUTFILE
But it printed me all the lines in the file. What was the wrong with this code? And how could I achieve what I want in a simple way with AWK?

I would thank you for your help!

Last edited by cristalp; 08-24-2012 at 10:36 AM.
 
Old 08-24-2012, 10:37 AM   #2
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
Code:
awk -v i=0 '/aaa/ {i=1} i==1' INPUTFILE

Last edited by byannoni; 08-24-2012 at 10:47 AM.
 
Old 08-24-2012, 10:40 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

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

Your awk seems to work well:
Code:
$ cat in
ssssss
oooooo
rrrrrr
aaa
111111
222222
333333
444444
555555
666666
END
$ awk '/aaa/{n=NR} NR > n+3 {print}' in
444444
555555
666666
END
BTW, you can omit {print} safely:
Code:
$ awk '/aaa/{n=NR} NR > n+3' in
because matched lines are printed by default.

EDIT: I got it.. n remains undefined (thus zero) until awk encounters /aaa/, so if you have more than 3 lines before 'aaa', they will be printed. Workaround:
Code:
$ cat in
ssssss
ssssss
ssssss
ssssss
oooooo
rrrrrr
aaa
111111
222222
333333
444444
555555
666666
END
$ awk '/aaa/{n=NR;} NR > n+3' n=1e5 in
444444
555555
666666
END
Here we initialize `n' to be large enough. Other, IMO more correct way is to check if n is not zero:
Code:
$ awk '/aaa/{n=NR;} n && NR > n+3' in

Last edited by firstfire; 08-24-2012 at 10:50 AM.
 
2 members found this post helpful.
Old 08-24-2012, 04:08 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
For the record, here's a sed solution as well.

Code:
sed -n '/aaa/,$ {/aaa/,+3 d;p}' infile
It first matches from line aaa to the end, then from that selection it deletes the aaa line and the three following it, and prints the remainder.

Note that the +" address option is, I believe, a gnu sed extension, and other implementations likely don't have it.


And to round it off, here are two ways to do it in ed too.

Code:
printf '%s\n' '1,/aaa/+3d' '%p' | ed -s infile
printf '%s\n' '/aaa/+4,$p' | ed -s infile
The first one deletes everything from line 1 to aaa+3, and prints what remains, while the second simply prints from line aaa+4 to the end (the rest of the lines are still in the buffer). As you can see, ed has a more flexible address system than sed.

If you replace the '%p/p' commands with 'w', you can save the changes directly back to the input file. Or use 'w outfile' to write them to a new file. The first version is probably slightly better for this.

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
2 members found this post helpful.
Old 08-28-2012, 04:51 AM   #5
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
Hi.

Your awk seems to work well:
Code:
$ cat in
ssssss
oooooo
rrrrrr
aaa
111111
222222
333333
444444
555555
666666
END
$ awk '/aaa/{n=NR} NR > n+3 {print}' in
444444
555555
666666
END
BTW, you can omit {print} safely:
Code:
$ awk '/aaa/{n=NR} NR > n+3' in
because matched lines are printed by default.

EDIT: I got it.. n remains undefined (thus zero) until awk encounters /aaa/, so if you have more than 3 lines before 'aaa', they will be printed. Workaround:
Code:
$ cat in
ssssss
ssssss
ssssss
ssssss
oooooo
rrrrrr
aaa
111111
222222
333333
444444
555555
666666
END
$ awk '/aaa/{n=NR;} NR > n+3' n=1e5 in
444444
555555
666666
END
Here we initialize `n' to be large enough. Other, IMO more correct way is to check if n is not zero:
Code:
$ awk '/aaa/{n=NR;} n && NR > n+3' in
Thanks!! This fully explains my question! Very clear. No confusion any more.
 
  


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
[SOLVED] Need help, can't figure it out! (awk/insert every nth line) sc0rpi0n Programming 15 04-19-2012 10:29 AM
[SOLVED] awk help (print matching line and next three) bradvan Programming 4 03-01-2012 05:11 AM
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM
print lines form nth line to mth line which fulfill specific condition cristalp Programming 4 11-07-2011 07:39 AM
print nth line after the line which matches the string cristalp Programming 7 10-27-2011 01:53 PM

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

All times are GMT -5. The time now is 08:20 AM.

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