- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: awk multiline record print
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2005 08:42 AM
тАО08-15-2005 08:42 AM
I have a multiline text file like below:
dn:djkjdf jdkjkjf
homedir kkjkjk
shell dkjkfjkgjg
userpasswd kkjk
dn:djkjdf jdkjkjf
homedir kkjkjk
shell dkjkfjkgjg
userpasswd kkjk
The file has hundreds of 4-line blocks like above. What I want is to output each 4-line block to each different file. If it is a four-line-record I want to print each record to a different file.
Please advise.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2005 09:06 AM
тАО08-15-2005 09:06 AM
Re: awk multiline record print
Here's a simple awk script inside a for loop that you can use to separate each of the 4-line blocks into separate output streams.
# for i in
> awk 'BEGIN{FS=""} {print $0 > "block"NR".out"}' $i
> done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2005 09:09 AM
тАО08-15-2005 09:09 AM
Re: awk multiline record print
# for i in
> awk 'BEGIN{RS=""} {print $0 > "block"NR".out"}' $i
> done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2005 09:17 AM
тАО08-15-2005 09:17 AM
SolutionIn a 'one-liner':
awk '/^dn/{n=NR;dn=$0;h=s=""}/^home/{h=$0}/^she/{s=$0}/^user/&&(NR-n==3){i++; f="xx_" i ".tmp"; print dn "\n" h "\n" s "\n" $0 > f }' < x
In a script use:
/^dn/{n=NR;dn=$0;h=s=""}
/^home/{h=$0}
/^shel/{s=$0}
/^user/&&(NR-n==3){
i++;
f="xx_" i ".tmp";
print dn "\n" h "\n" s "\n" $0 > f
}
What happens?
- If we see a line starting with dn then remember the line number, remember the line in dn, clear home and shell variables.
- Remember home and shell in h and s if you see them
- If you see a line starting with user, and it is 3 lines after dn, then
-- increment the file number
-- generate a fresh file name
-- print all four line onto the new file name
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2005 09:26 AM
тАО08-15-2005 09:26 AM
Re: awk multiline record print
Use the awk construct within a for loop only if you want to operate on multiple files within a directory. Otherwise the awk statement by itself will do what you want:
# awk 'BEGIN{RS=""} {print $0 > "block"NR".out"}'
cheers!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2005 09:36 AM
тАО08-15-2005 09:36 AM
Re: awk multiline record print
"If it is a four-line-record"
This is critical. How do you define that.
My first solution is a fairly rigirous test, only creating a new file under specific conditions.
If dor example you 'know' that there will always be a four-line-record as soon as you see a line starting with 'dn:' then the solution simplyfies a lot. For example:
awk '/^dn:/{x=4;i++;f="xx_"i".tmp"; while (x--) {print $0>f; getline} }' < x
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2005 06:01 PM
тАО08-16-2005 06:01 PM
Re: awk multiline record print
yashy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2005 08:20 PM
тАО08-16-2005 08:20 PM
Re: awk multiline record print
# awk '/^dn:/{x=4;i++;f="xx_"i".tmp"; while (x--) {print $0>f; getline} }' < x
a) It will read from a file called x
b) awk will search a line with dn string
c) It will collect 4 lines from the line starting with dn:
d) New file name is assigned as, xx_1.tmp 1 will be increased with i variable. This file is assigned to f variable.
e) print $0 > f will write as, print $0 > xx_1.tmp
File name will be varied with 1..n.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-17-2005 08:05 AM
тАО08-17-2005 08:05 AM
Re: awk multiline record print
Are there empty lines in the file ? If not, why not use split -l 4 (split a file into 4-lines pieces).
If there are empty lines, supress those lines (there is several ways to do that) and then use split.
If it not is possible to just to supress lines, use a simple script as:
awk '/^dn/ {print $0; getline; print $0; getline; print $0; getline; print $0;}' file >outfile
and, then use split -l 4 on outfile.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2005 05:21 PM
тАО08-22-2005 05:21 PM
Re: awk multiline record print
Thanks Leif. I already know about the split option and I was actually looking for an awk option. Thanks again everyone.
Yashy