Solved

Split a single file into 4 separate files one for each column of data using perl or shell

Posted on 2011-09-02
4
397 Views
Last Modified: 2012-05-12
If I have a file that looks like this below with any number of rows. I want to create 4 files from the sample input file. It would simply keep the date & timestamp then a single column corresponding to the new file name for each file. The new files with have 3 columns of data each. The date, timestamp, & column of data.

/path1/inputfile.txt
08-14-2011 00:00:00 0 30 0 23
08-15-2011 00:00:00 1 10 9 12
08-19-2011 00:00:00 0 07 0 18
08-20-2011 00:00:00 0 13 0 11

/path2/outputfile1.txt
08-14-2011 00:00:00 0
08-15-2011 00:00:00 1
08-19-2011 00:00:00 0
08-20-2011 00:00:00 0

/path2/outputfile2.txt
08-14-2011 00:00:00 30
08-15-2011 00:00:00 10
08-19-2011 00:00:00 07
08-20-2011 00:00:00 13

/path2/outputfile3.txt
08-14-2011 00:00:00 0
08-15-2011 00:00:00 9
08-19-2011 00:00:00 0
08-20-2011 00:00:00 0

/path2/outputfile4.txt
08-14-2011 00:00:00 23
08-15-2011 00:00:00 12
08-19-2011 00:00:00 18
08-20-2011 00:00:00 11
0
Comment
Question by:libertyforall2
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 250 total points
ID: 36476795
Try this for size:

use strict;
use FileHandle; 
my @fds;
while( <> ) {
   chomp;
   my @fields = split;
   if( $. == 1 ) {
      foreach my $field (2..$#fields) {
	 my $fdn = $field-1;
	 my $filename =  "/tmp/fill_$fdn.txt";
	 my $fd = FileHandle->new("> $filename") || die "Can't open $filename: $!\n";
	 $fds[$field-1] = $fd ;
	 print "Opened $filename\n";
      }
   }
   foreach my $field (2..$#fields) {
      my $fd = $fds[$field-1];
      print( $fd "@fields[0,1] $fields[$field]\n");
   }
}

Open in new window

0
 
LVL 10

Expert Comment

by:jeromee
ID: 36476799
run it as:
 perl /path/script.pl  /tmp/data.txt
0
 
LVL 13

Assisted Solution

by:Carl Bohman
Carl Bohman earned 250 total points
ID: 36476829
Or, if you like one-liners:
perl -ne 'BEGIN{foreach $n(1..4){open(my $F,">/path2/outputfile$n.txt"); $Files[$n]=$F}}@S=split;foreach $n(1..4){print {$Files[$n]} join(" ", @S[0,1,$n+1]), "\n"}' /path1/inputfile.txt

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 36477020
One liner worked great!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question