Operating System - Linux
1748051 Members
4834 Online
108758 Solutions
New Discussion юеВ

Re: Parsing a single file into multiple files

 
SOLVED
Go to solution
john guardian
Super Advisor

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.
12 REPLIES 12
Sandman!
Honored Contributor

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.
john guardian
Super Advisor

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.
James R. Ferguson
Acclaimed Contributor
Solution

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...
Sandman!
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor

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...
Gilles Allard
Advisor

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
James R. Ferguson
Acclaimed Contributor

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...
john guardian
Super Advisor

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.
john guardian
Super Advisor

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.