LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [Perl] append columns to file (https://www.linuxquestions.org/questions/programming-9/%5Bperl%5D-append-columns-to-file-526669/)

noir911 02-08-2007 04:44 AM

[Perl] append columns to file
 
My script copies stuff from one file to another. After the copy is done to the new file I want to append 2 new columns.

Currently it's copying a table from one file to another; eg.

Quote:

proto stats bytes

all open 1500

all closed 4556

all closed 4443

all closed 2457
I want to append 2 new columns/ rows to the new file

Quote:


Company State

Name1 WA

Name 2 MD

Name 3 NY

Here's my existing code

Code:


#!/usr/bin/perl -w
my $file="file.log";
my $nf="nf.log";
open NEWFILE, ">>$nf" or die "Can't open $nf";
open (FILE, "$file") or die ("can't open this: $!");
my @lines=<FILE>;
print NEWFILE @lines;
close (FILE) or die "FILE can't be closed: $!";
close NEWFILE;


druuna 02-08-2007 05:00 AM

Hi,

I'm not sure what it is you want to do.

- Is the new data also in a (separate) file?
- Does the new data need to be appended to the end of the outfile ($nf in your case)?

Also: I don't know if you are 'just playing around' with perl or have something else in mind, but there are easier ways to copy/append files. If you have a problem that needs to be solved, maybe you could tell us what you want to accomplish in the end.

noir911 02-08-2007 05:15 AM

Quote:

Originally Posted by druuna
- Is the new data also in a (separate) file?

The new data _can be_ in a separate file. Say, new.txt.

Quote:

Originally Posted by druuna
- Does the new data need to be appended to the end of the outfile ($nf in your case)?

Yes. I need to append the new data to the end of $nf.

Quote:

Originally Posted by druuna
Also: I don't know if you are 'just playing around' with perl or have something else in mind, but there are easier ways to copy/append files. If you have a problem that needs to be solved, maybe you could tell us what you want to accomplish in the end.

Not playing around. I need to parse some firewall logs, append some new stuff (State names) and get a final result. I'm trying to solve it one piece at a time. I will share the entire code once it's done.

druuna 02-08-2007 05:29 AM

Hi again,

Appending to an already existing file is not to hard:
Code:

#!/usr/bin/perl -w

my @lines;

# Infiles
my $file1="data1.log";
my $file2="data2.log";

# Outfile
my $nf="nf.log";

# Open outfile
open NEWFILE, ">$nf" or die "Can't open $nf";

# Open first infile and write to outfile
open (FILE, "$file1") or die ("can't open this: $!");
@lines=<FILE>;
print NEWFILE @lines;
close (FILE) or die "FILE can't be closed: $!";

# Open and append second infile to outfile
open (FILE, "$file2") or die ("can't open this: $!");
@lines=<FILE>;
print NEWFILE @lines;
close (FILE) or die "FILE can't be closed: $!";

# Close outfile
close NEWFILE;

The above code will put lines from data1.log into nf.log and then put (append) lines from data2.log to nf.log. In the end nf.log holds both data1.log and data2.log lines.

This is just one way of doing what you ask. Depending on what you need to do with the lines from data1 and/or data2 other solutions could be better/faster. Here's a variation on the same script:
Code:

#!/usr/bin/perl -w

# Infiles
my $file1="data1";
my $file2="data2";

# Outfile
my $nf="nf.log";

# Open first infile and read contens
open (FILE, "$file1") or die ("can't open this: $!");
my @lines01=<FILE>;
close (FILE) or die "FILE can't be closed: $!";

# Open second file and read contens
open (FILE, "$file2") or die ("can't open this: $!");
my @lines02=<FILE>;
close (FILE) or die "FILE can't be closed: $!";

# Open outfile
open NEWFILE, ">$nf" or die "Can't open $nf";

# print content of both files to outfile.
print NEWFILE @lines01;
print NEWFILE @lines02;

# Close outfile
close NEWFILE;



All times are GMT -5. The time now is 06:06 PM.