LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-20-2013, 02:31 AM   #1
juliej
LQ Newbie
 
Registered: Jul 2010
Posts: 9

Rep: Reputation: 2
Question Is it possible to output 38 lines in between x & y on a text file?


I'm using Debian, remote only access.
I have a long text file that I want to read/output to my console, but only the 38 lines that are between x and y.
The 'location' of these lines within the file might change but the lines themselves do not change.
Is there a command to output to my console only the 38 lines that is 'in between' these two lines:

%d blah blah blah%a
(38 lines of text here)
blah blah: %d

Thank you so much

Last edited by juliej; 03-20-2013 at 02:35 AM.
 
Old 03-20-2013, 03:02 AM   #2
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Hi juliej,

If i got you correct,
Quote:
grep "%d blah blah blah%a" <path_to_file_name> -A 38
Will return you the next 38 lines after the line %d blah blah blah%a in the file.
 
1 members found this post helpful.
Old 03-20-2013, 03:26 AM   #3
juliej
LQ Newbie
 
Registered: Jul 2010
Posts: 9

Original Poster
Rep: Reputation: 2
/me jumps up and hugs RaviTezu
thank you That is perfect!

I was over thinking it with the 'in between' hehe.

btw did I say THANK YOU
 
Old 03-20-2013, 07:54 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Some other options:

Using gnu sed:
Code:
sed -n ' /pattern/,+38p' infile.txt
Using ed:
Code:
ed -s infile.txt <<<'/pattern/;+38p'
Only gnu sed has the "+n" address pattern. In ed, separating the two addresses with ';' makes the second address relative to the first address. If you use a comma it's relative the current line.


If you want to get the part between the two patterns, whatever the number of lines, and with or without the bracketing lines:

Code:
#includes bracketing lines:
sed -n '/pattern1/,/pattern2/p' infile.txt
ed -s infile.txt <<<'/pattern1/;/pattern2/p'


#excludes bracketing lines:
sed -n '/pattern1/,/pattern2/ {/pattern1\|pattern2/d;p}' infile.txt
ed -s infile.txt <<<'/pattern1/+1;/pattern2/-1p'
As you can see, ed is generally better when you have to deal with relative line positions. With sed you have to process the matched section to remove the lines you don't want.

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.catonmat.net/series/sed-one-liners-explained

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)
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Reformatting text output to join lines inversecow Programming 4 01-18-2012 06:25 PM
[SOLVED] Redirecting terminal output to text file leads to corrupted text file mariovrn Linux - Newbie 5 11-30-2011 07:21 AM
need help to slit multiple lines of text output at whitspaces in shell Ryanjon7 Programming 4 10-20-2011 09:44 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 10:59 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM


All times are GMT -5. The time now is 02:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration