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
398 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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Read about achieving the basic levels of HRIS security in the workplace.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

740 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