Re: Parsing a single file into multiple files (199 Views)
Reply
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 1 of 13 (199 Views)
Accepted Solution

Parsing a single file into multiple files

I have a file . It has multiple patch listings within, for example line 1 might start out as "s700 10.10". Line 33 (the line number will vary with every new patch file) might start out as "s700 10.26". This file contains "listings" of files for multiple HPUX Operating Systems. I'd like to break it up so that each individual file contains only the list of patches for a particular level of OS (like s700 10.10). Can this be done w/CSPLIT or do I have to use something else? Thanks.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 2 of 13 (199 Views)

Re: Parsing a single file into multiple files

Imho it would help if you could you provide a sample of the input as well as the desired output.
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 3 of 13 (199 Views)

Re: Parsing a single file into multiple files

No problem. Here it is...........
If you view the contents, it's a simple format. I just want to break it up into patch lists for each individual OS. Thanks.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 13 (199 Views)

Re: Parsing a single file into multiple files

Hi John:

Indeed, why not use 'csplit'. Given this file:

# cat ./patches
s700 10.10
PHCO_123
PHCO_456
s700 10.20
PHNE_12345
PHNE_22222
s800 11.0
PHKL_18543
PHSS_1234
PHSS_2222

...Using:

# csplit -s -f patchesfor ./patches '/s700 10.10/' '/s700 10.20/' '/s800 11.0/'

...produces files named "patchesfor00", "patchesfor01", "patchesfor02" and "patchesfor03". The first file is empty since there is nothing preceeding the first regular expression argument in the input file.

Regards!

...JRF...
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 5 of 13 (199 Views)

Re: Parsing a single file into multiple files

How about using an awk construct which puts an OSes records into one file:

# awk '{print $0 >> "patch."$3}' file

~cheers
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 13 (199 Views)

Re: Parsing a single file into multiple files

Hi (again) John:

It seems that a generalized 'csplit' something like:

# csplit -s -f patchesfor patches.txt '/^s/' '/^$/' '{35}'

...which should create files named 'patchesfor00'... dividing them into groups that begin with "s" [for s700 or s800] and ending with a blank line as the file you provide has, should work.

*However*, on my servers, I get an "{35} - out of range" error. You have thirty-one (31) files to generate given your data.

Sandman's suggestion is elegant (as his 'awk' always is!), but with your data, it doesn't behave as well as it should.

For example, within your stanza for s800 11.23 you have variously "11.23" and "11.X". I would think that you would still like to keep these grouped within the same output file.

*Hence*, I'd deploy a Perl solution like this:

# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
my $fh;
while (<>) {
if (/^s/) {
my @f = split( /[:\s]/, $_ );
my $file = "$f[0]_$f[1]";
open( $fh, ">>", $file ) or die "Can't open $file: $!\n";
}
print $fh $_;
}
1;

...Run as:

# ./filter patches.txt

Your output files will be produced in the directory in which you run this script. The files will be named like "s700_20.20", "s800_11.23", etc. based upon the first two fields (s700 or s800 and the release ID following it).

Regards!

...JRF...
Advisor
Gilles Allard
Posts: 27
Registered: ‎07-01-2001
Message 7 of 13 (199 Views)

Re: Parsing a single file into multiple files

Simple needs require simple solutions. I prefer the following shell script:

FILE=/dev/null
while read LINE; do
if [ -z "${LINE}" ]; then continue; fi
if [ -z "${LINE##s*}" ]; then
FILE=`echo $LINE|tr -s " " "_"`
else
echo $LINE >> $FILE
fi
done
Blank lines will be stripped.
Filenames will be as in JRF post
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 13 (199 Views)

Re: Parsing a single file into multiple files

Hi John:

By the way, would you please evaluate the responses you received in an earlier post, too. Thanks!

https://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1063457

Regards!

...JRF...
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 9 of 13 (199 Views)

Re: Parsing a single file into multiple files

I eval'd the earlier responses early yesterday. If they weren't updated, I'll have to redo. I assigned points and then closed that thread yesterday. I 'll recheck, though. Thanks.
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 10 of 13 (199 Views)

Re: Parsing a single file into multiple files

Thanks to everyone for the input. They all worked fine. The scipt is now working well. Regards.

BTW, if anyone hasn't rx'd points, let me know. I believe all w/script suggestions ahve been assigned. Thanks again.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 11 of 13 (199 Views)

Re: Parsing a single file into multiple files

Hi John:

The thread I would like you to look at dates from September:

https://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1063457

Happy New Year!

Regards!

...JRF...
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 12 of 13 (199 Views)

Re: Parsing a single file into multiple files

On closer inspection I agree with JRF that the awk construct I posted earlier will not do the job owing to the variable format of field three in the input file. Besides I used a small portion of the sample input you had provided to test my script. So I used the entire input file provided, built another script that provides the desired output. And thank you for those words JRF, coming from the master himself :)

Give the awk script below a try:

# awk -F" +" '{if($0!~"^ " && $0!~"^$")p=z[split($2,z,":")-1];print $0>>"patch."p}' file

~cheers
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 13 of 13 (199 Views)

Re: Parsing a single file into multiple files

Thanks guys. All input appreciated. I'm off and running now. See you all in the New Year!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.