Creating output file using Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating output file using Perl
# 1  
Old 01-14-2010
Creating output file using Perl

As an simple example, I have the following files

fin1.zv being a one column set of number

Code:
1 90
2 80
3 60
4 30
5 20

fin2.zv is another file like this

Code:
1 10 20 30 40 50
2 60 70 80 90 0
3 90 80 70 60 50
4 40 30 20 10 0
5 10 20 30 40 50

Want to do $i - $2 / $2, let's say we store it in var[i]

var = $i - $2 / $2

where $2 is second column from the first file and $i is the ith column from the second file with i = 2 to i = NF

Want to output a file called file2.zp like this

$1 var[2] var[3] var[4] var[5] ...

where $1 is from file fin2.zv


I have a perl code which does this when I don't have the first column in the files.

Code:
    perl -e ' open(F1,"<$1"); open(F2,"<$2");  \
      while( ($p1=<F1>) && ($p2=<F2>) ) {      \
        foreach (split(/\s+/,$p2)) {           \
          print ($_ - $p1) / $p1," ";          \
        }                                      \
        print "\n"                             \
      }                                        \
      close(F1); close(F2); '                  \
      < fin1.zv fin2.zv                  \
      > fin2.zp

So I need to change it so that it takes account that the files now contain the additional first column as described and output the correct format I mentioned.
# 2  
Old 01-15-2010
Please give an example of the desired output based on the data samples above.
# 3  
Old 01-15-2010
Tools

Quote:
Originally Posted by kristinu
As an simple example, I have the following files

fin1.zv being a one column set of number

Code:
1 90
2 80
3 60
4 30
5 20

fin2.zv is another file like this

Code:
1 10 20 30 40 50
2 60 70 80 90 0
3 90 80 70 60 50
4 40 30 20 10 0
5 10 20 30 40 50

Want to do $i - $2 / $2, let's say we store it in var[i]

var = $i - $2 / $2

where $2 is second column from the first file and $i is the ith column from the second file with i = 2 to i = NF

Want to output a file called file2.zp like this

$1 var[2] var[3] var[4] var[5] ...

where $1 is from file fin2.zv


I have a perl code which does this when I don't have the first column in the files.

Code:
    perl -e ' open(F1,"<$1"); open(F2,"<$2");  \
      while( ($p1=<F1>) && ($p2=<F2>) ) {      \
        foreach (split(/\s+/,$p2)) {           \
          print ($_ - $p1) / $p1," ";          \
        }                                      \
        print "\n"                             \
      }                                        \
      close(F1); close(F2); '                  \
      < fin1.zv fin2.zv                  \
      > fin2.zp

So I need to change it so that it takes account that the files now contain the additional first column as described and output the correct format I mentioned.
Hello,
I think this has already been answered by someone on the forum apparently by me only.

P.S.
Enough of Free Service...!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

creating separate output file for each input file in python

Experts, Need your help for this. Please support My motive is to create seperate output file for each Input Files(File 1 and File2) in another folder say(/tmp/finaloutput) Input files File 1(1.1.1.1.csv) a,b,c 43,17104773,3 45,17104234,4 File 2(2.2.2.2.csv) a,b,c 43,17104773,1... (2 Replies)
Discussion started by: as7951
2 Replies

2. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Splitting a file and creating new files using Perl script

Hi All, I am new to Scripting language. I want to split a file and create several subfiles using Perl script. Example : File format : Sourcename ID Date Nbr SU IMYFDJ 9/17/2012 5552159976555 SU BWZMIG 9/14/2012 1952257857887 AR PEHQDF 11/26/2012 ... (13 Replies)
Discussion started by: Deepak9870
13 Replies

4. Shell Programming and Scripting

Perl- creating a matrix from a 3 column file

Dear all, I'm new in perl scripting and I'm trying to creating a matrix from a 3 column file sorting data in a particular manner. In the final matrix I need to have the first column "IDs" on the header of the columns and the second column values on the header of each row. And the value fo the... (2 Replies)
Discussion started by: gabrysfe
2 Replies

5. Shell Programming and Scripting

creating excel file using perl

Hi , I am writing a simple excel file and want to create the file under say 'D:\Documents and Settings'. The problem with my code is it is writing in the same directory instead of the specified. Here is a sample code use Spreadsheet::WriteExcel; my $workbook =... (1 Reply)
Discussion started by: daptal
1 Replies

6. Shell Programming and Scripting

Help with creating a text file in perl with file creation date.

Hi, I am quite new to Perl scripting and i need to create a .TXT file using perl, with fields (A,B,C,D,E), and this text file should be named with current file creation date "XYZ_CCYYMMDD.TXT" (i.e.XYZ_2011042514:33 PM). Can anyone who has done this, please share their expertise on this... (5 Replies)
Discussion started by: msrahman
5 Replies

7. UNIX for Dummies Questions & Answers

Diff creating special characters in output file

Hi I am comparing 2 files (using diff command) with numerical data in them. In the output file I want only the differences which are in file2 but not in file1. Although I am getting the diffences i am also getting special characters in the output file which i do not want. Can somebody help... (2 Replies)
Discussion started by: ashu_r2001
2 Replies

8. UNIX for Dummies Questions & Answers

Creating a file that contains output from a command, and then displays itself

hey, I'm trying to create the command that will create a file named user.txt that contains the output of the command cut -d: -f1,5 /etc/passwd, and displays itself afterwards. I don't know how to bridge cat > user.txt with cut -d: -f1,5 /etc/passwd, or how display it afterwards. Any help would... (2 Replies)
Discussion started by: raidkridley
2 Replies

9. Shell Programming and Scripting

Append Output to another file in Perl

Hi All, I am writing a Perl script such that the output from "perl myscript.pl file1" to be appended to another file name called file2. I tried out with the below code but couldn't work. Can any expert give me some advice? open(OUTPUT, 'perl myscript.pl file1 |'); close OUTPUT;... (7 Replies)
Discussion started by: Raynon
7 Replies

10. Shell Programming and Scripting

creating a file using Perl

Hi guys, when i use the following code: open(DEST, ">>DESTINATION_")|| die "$!"; #OPEN FILE FOR WRITING @contents=@field; print DEST "@contents\n"; I am creating a file that lookss like this: Wed May 14 11:42:03 2008 10800 306973223399 19419-NEA 1 1259 1 3 80 What i want to do is... (1 Reply)
Discussion started by: chriss_58
1 Replies
Login or Register to Ask a Question