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 04-11-2012, 01:48 AM   #1
pravin.bhande
LQ Newbie
 
Registered: Jul 2010
Location: PUNE
Distribution: RHWS 4.0 , Mandrake 10 , Suse 11 , CentOS 5.0
Posts: 1

Rep: Reputation: 0
Thumbs up Printing $ Lines After Finding Specific Pattern


Hi,

Following code block will help you to print lines/values after locating specific pattern in the file

i :- Key/search pattern
X :- any number e.g. 1 or 2
Y :- any number

1. Printing Xth line after finding specific pattern

sed -n '/'${i}'/,$p' <File> | sed -n 'Xp'

2. Printing X lines after finding specific pattern

sed -n '/'${i}'/,$p' <File> | sed -n 'X,Yp'

here X and Y are start to end number

Regards,
Pravin B

Last edited by pravin.bhande; 04-22-2012 at 04:02 AM. Reason: renamed 'files'to 'lines'.
 
Old 04-11-2012, 04:28 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So is there a question here or you are just showing your skills?
 
Old 04-11-2012, 09:41 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by pravin.bhande View Post
Following code block will help you to print lines/values after locating specific pattern in the file

i :- Key/search pattern
X :- any number e.g. 1 or 2
Y :- any number

1. Printing Xth file after finding specific pattern

sed -n '/'${i}'/,$p' <File> | sed -n 'Xp'

2. Printing X files after finding specific pattern

sed -n '/'${i}'/,$p' <File> | sed -n 'X,Yp'

here X and Y are start to end number
Thank you for this contribution. A gentle word of constructive criticism regarding terminology.

Code:
1. Printing Xth file after finding specific pattern
 .. should be ...
1. Printing Xth line after finding specific pattern
... and ...
Code:
2. Printing X files after finding specific pattern
 .. should be ...
2. Printing lines X through Y after finding specific pattern
Daniel B. Martin
 
Old 04-12-2012, 08:03 AM   #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
Good work. Figuring out how to do such things on your own is always a good thing to do.

That said, however, with gnu sed (the Linux standard) and a few others, it's possible to do the same things in a single command. It includes additional address range forms for matching numbers of lines after the initial match.

To print line $i, plus $n lines after it:

Code:
i=pattern
n=4

sed -n "/$i/,+$n p" file.txt

#or just use grep instead:
grep -A "$n" "$i" file.txt
(BTW, as you can see, to use a variable in the expression you can usually just enclose the whole thing in double quotes.)


To print only the $n'th line after the pattern, use a nested expression to delete all the lines from the first match that you don't want:

Code:
i=pattern
n=4
m=$(( n - 1 ))

sed -n "/$i/,+$n { /$i/,+$m d; p }" file.txt

Last edited by David the H.; 04-12-2012 at 08:05 AM. Reason: minor edits
 
1 members found this post helpful.
Old 04-12-2012, 08:49 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by David the H. View Post
To print line $i, plus $n lines after it ...
OP showed how to not print line $i, and print only $n lines after it. Please post a version of your excellent code which does this.

Daniel B. Martin
 
Old 04-12-2012, 10:00 AM   #6
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
Easy. Just use the same technique as in my second example, but delete only the line with the pattern. Or invert the match and print everything except it.

Code:
sed -n "/$i/,+$n { /$i/d; p }" file.txt
sed -n "/$i/,+$n { /$i/!p }" file.txt
 
1 members found this post helpful.
Old 04-12-2012, 10:20 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by David the H. View Post
Code:
sed -n "/$i/,+$n { /$i/d; p }" file.txt
sed -n "/$i/,+$n { /$i/!p }" file.txt
Works like a charm. Thank you.

Daniel B. Martin
 
  


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
Copy lines starting and ending with specific pattern from multiple files to a file ssn Linux - Newbie 2 07-27-2011 10:44 AM
deleting lines from a file with specific pattern using AWK gandhigaurav1986 Programming 12 06-08-2010 02:08 AM
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 01:11 AM
Printing specific lines of a file using script. barunparichha Linux - Software 6 05-20-2009 12:31 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM

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

All times are GMT -5. The time now is 11:51 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