LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-08-2007, 04:44 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
[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;

Last edited by noir911; 02-08-2007 at 04:46 AM.
 
Old 02-08-2007, 05:00 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 02-08-2007, 05:15 AM   #3
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-08-2007, 05:29 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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;

Last edited by druuna; 02-08-2007 at 05:30 AM.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Perl] append to new file noir911 Programming 5 02-08-2007 06:38 AM
how to exchange two columns of a file jackk294 Programming 8 07-11-2006 11:37 PM
Reading in data in columns from a file (C++) Nylex Programming 4 03-08-2006 05:55 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration