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
395 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
Comment Utility
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
Comment Utility
run it as:
 perl /path/script.pl  /tmp/data.txt
0
 
LVL 13

Assisted Solution

by:Carl Bohman
Carl Bohman earned 250 total points
Comment Utility
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
Comment Utility
One liner worked great!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now