Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 1000 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 1000 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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

722 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