Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-20-2013, 03:31 AM   #1
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 03:35 AM.
Old 03-20-2013, 04:02 AM   #2
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Hi juliej,

If i got you correct,
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, 04:26 AM   #3
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, 08:54 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Some other options:

Using gnu sed:
sed -n ' /pattern/,+38p' infile.txt
Using ed:
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:

#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:

How to use ed:
(also read the info page)


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 07:25 PM
[SOLVED] Redirecting terminal output to text file leads to corrupted text file mariovrn Linux - Newbie 5 11-30-2011 08:21 AM
need help to slit multiple lines of text output at whitspaces in shell Ryanjon7 Programming 4 10-20-2011 10:44 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11: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 03:36 PM > Forums > Linux Forums > Linux - Newbie

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

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