Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep all lines between certain lines 1

Status
Not open for further replies.

buckpasser

Technical User
Mar 10, 2005
24
US
Folks,

I would like to grep a file such that lines between certain lines are saved to a file.

For instance, grep/save lines between lines starting with "#" and lines starting with ":"

Thanks
 
Hi

Must be [tt]grep[/tt] ? [tt]sed[/tt] or [tt]awk[/tt] would be easier.
Code:
sed -n '/^#/,/^:/p' inputfile > outputfile
[gray]# or[/gray]
awk '/^#/,/^:/' inputfile > outputfile
If you search the archive, you will find a lot of threads about this.

Feherke.
 
Hi,

ex /path/to/input_file1>/dev/null 2>&1 <<- EOEX
$
?^#
+1
.,/^:/-1 w! /path/to/output_file
EOEX
 
Hi

That is nice, aau. Looks misterious too, at least for me.

But I do not understand a thing. You wrote [tt]?[/tt] for the search, and this searches backward wrapping around the end of file and find the last # line. Is this the normal/intended way, or just I missed something while tried to follow the steps of your code ?

Feherke.
 
feherke's awk was quality, so thank you feherke, and a star.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi feherke

Yes the search backward is intended when you have more than one line beginning with #.
bukpasser said <lines starting with # and lines starying with : >

I understand with this tha the file may present some lines like this :

1111
2222
#33333
#44444
aaaa
bbbb
cccc
:eeee
:ffff

only lines aaaa to cccc will be concerned.

That's is why the last occurrence is searched.
 
Hi

So that is the theory, thanks. Never thinked to this possibility. I looked dumb to your code, because I used abit different input file :

one
# two
three
: four
five
# six
seven
eight
: nine
ten

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top