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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract lines between delimiter to file based on first field of record 1

Status
Not open for further replies.

blarneyme

MIS
Jun 22, 2009
160
US
Have a text file that contains:
Code:
xhost1
line with text
second line with text
third line with text
=======================
zhost2
line with text
second line with text
third line with text
fourth line with text
fifth line with text
======================
zhost3
line with text
second line with text
======================
jail4
line with text
second line with text
third line with text
======================

Want to extract all lines between "=================" and put the contents into a file name ([x|z]host|jail)[N].txt for each host.

Note the host may be "xhost" "zhost" or "jail"

For example:
# cat xhost1.txt
line with text
second line with text
third line with text

#cat zhost2.txt
line with text
second line with text
third line with text
fourth line with text
fifth line with text

Code:
awk '
        { RS = "================================="; FS = "\n" }
        ($1 == "^xhost") {print $0}
' myfile > $1.txt

Unfortunately all I get if print to stdout is the hostname (host1) and that is it. If printing to the file ($1.txt) I'm not getting any file.

Thanks
 
awk '/^======/{next}/^([xz]host|jail)/{out=$1".txt";next}{print >out}' myfile

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top