Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

creating smaller files from one large one

by Angharad (Pilgrim)
on Jun 08, 2009 at 17:08 UTC ( [id://769615]=perlquestion: print w/replies, xml ) Need Help??

Angharad has asked for the wisdom of the Perl Monks concerning the following question:

I have a file that looks like this
item1 item2 item3 0 0 1 S 0 0 0 0 2 K 0 0 0 0 23 T 1 M 12 T 24 N 2 L 45 0 36 V 14 I 46 0 38 W 16 R 51 S 39 A 17 L 52 A 42 N 20 E 53 0 43 L 21 G
So, for each item, there are two columns of data. My question is, how might I go about writing new files that only contain the two columns for that particular item (and name each file item1.dat, item2.dat etc.) So, for example, item1.dat should look like this:
0 0 0 0 0 0 12 T 45 0 46 0 51 S 52 A 53 0
The actual original file is a fair bit larger than this and has more items. All of these items need to have their own files containing their two items of data. Any advice as to how to get started etc appreciated.

Replies are listed 'Best First'.
Re: creating smaller files from one large one
by jwkrahn (Abbot) on Jun 08, 2009 at 17:29 UTC
    #!/usr/bin/perl use warnings; use strict; # process headers, create filehandles my @fhs; for my $header ( split ' ', <DATA> ) { my $file = "$header.dat"; open my $FH, '>', $file or die "Cannot open '$file' $!"; push @fhs, $FH; } while ( <DATA> ) { my @fields = /\S+\s+\S+/g; @fields == @fhs or die "Error: incorrect number of fields in recor +d $.\n"; for my $fh ( @fhs ) { print $fh shift( @fields ), "\n"; } } __DATA__ item1 item2 item3 0 0 1 S 0 0 0 0 2 K 0 0 0 0 23 T 1 M 12 T 24 N 2 L 45 0 36 V 14 I 46 0 38 W 16 R 51 S 39 A 17 L 52 A 42 N 20 E 53 0 43 L 21 G
Re: creating smaller files from one large one
by toolic (Bishop) on Jun 08, 2009 at 17:28 UTC
    open three files for output, loop through your input file, then print to your output files:
    use strict; use warnings; open my $fh1, '>', 'item1.dat' or die "can not open file item1.dat: $! +"; open my $fh2, '>', 'item2.dat' or die "can not open file item2.dat: $! +"; open my $fh3, '>', 'item3.dat' or die "can not open file item3.dat: $! +"; while (<DATA>) { next if (/^item/); # ignore header my @items = split; print $fh1 "@items[0..1]\n"; print $fh2 "@items[2..3]\n"; print $fh3 "@items[4..5]\n"; } __DATA__ item1 item2 item3 0 0 1 S 0 0 0 0 2 K 0 0 0 0 23 T 1 M 12 T 24 N 2 L 45 0 36 V 14 I 46 0 38 W 16 R 51 S 39 A 17 L 52 A 42 N 20 E 53 0 43 L 21 G
Re: creating smaller files from one large one
by ikegami (Patriarch) on Jun 08, 2009 at 17:11 UTC
    perl -ple's/^(\S+\s+\S+).*/$1/' infile > outfile
    Or with 5.10+,
    perl -ple's/^\S+\s+\S+\K//' infile > outfile

    Filter through the following to remove the header line:

    perl -ne'print if $.>1'

    Update: ah, you don't want to lose the extra columns, oops!

    chomp( my $header = <> ); my @headers = $header =~ /\S+/g; my @fhs; for (@headers) { my $qfn = "$_.dat"; open(my $fh, '>', $qfn) or die("Can't create file \"$qfn\": $!\n"); push @fhs, $fh; } while (<>) { chomp; my @fields = split(/\s+/, $_); for (0..$#fhs) { printf({ $fhs[$_] } "%s %s\n", $fields[$_*2 + 0], $fields[$_*2 + 1], ); } }
Re: creating smaller files from one large one
by jvector (Friar) on Jun 09, 2009 at 22:41 UTC
    For me, this is one of the joys of this site. One question, three totally different answers; each a delight for its elegance, clarity, or economy. What a vehicle for communication. I look forward to the day when I may offer solutions of such quality.

    This signature reduced by 50% this week.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://769615]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-18 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found