LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-10-2009, 05:59 AM   #1
shaife720
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Rep: Reputation: 0
Perl - Adding new line on the beginig of a file


Hi
I have a Perl script that in the end creates file , name of the file is history containing 4 lines .
After the script finishes creating this history file it sends it to a hobbit server with a diffrent command.
I need before the sending command to add empty line to the history file before the other 4 lines how can I do it and what do I need to write .

You can see below the part of the script where I need to new line action to be done.

open(Infile,"</usr/local/hobbit/server/ext/history") || die "Cannot open file\n";
my $RedAlertCommand = "/usr/local/hobbit/client/bin/bb 172.50.40.10:2984 \"status eur-dmrm06.us.ensrv.mgmt.rsa.trap red `date` `cat
/usr/local/hobbit/server/ext/history` \"";
print $RedAlertCommand,"\n";
my $GreenAlertCommand = system ("/usr/local/hobbit/client/bin/bb 172.50.40.10:2984 \"status eur-dmrm06.us.ensrv.mgmt.rsa.trap green
`date` `cat /usr/local/hobbit/server/ext/history` \"");
if(eof(Infile)){
system ("$GreenAlertCommand");
}else{
system ("$RedAlertCommand");
}
close Infile;

Please advise

Regards
Shai
 
Old 08-10-2009, 06:56 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,438
Blog Entries: 11

Rep: Reputation: 182Reputation: 182
I'm not quite sure where you want to prepend the 4 empty lines. Is it the file /usr/local/hobbit/server/ext/history or is it the file that you want to send over to the hobbit server?
 
Old 08-10-2009, 07:15 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
The code snippet that you have provided read the file, Please provide the snippet where you are writing to the file.
 
Old 08-12-2009, 05:23 AM   #4
shaife720
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Hi
Its not 4 empty file.
I have history file that contains 4 actual lines with data and this is the base status .
In this attached script before the first line I need to write something that will manipulate the history file and will add an empty line inside the history file before the current actual 4 lines with data in the history file :
Shai
 
Old 08-12-2009, 06:35 AM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
It's not especially hard to prepend information. You can do it by in-place editing of one file, but what I usually do is open the old file for reading, open a new file for writing, add the new information to the new file and then copy everything from the old file into the new file. After that, assuming it all goes well, you can switch the names easily (via Perl or via the shell) so that the old file becomes "file.bak" and the new file gets the name of the old file. If you do that all within one Perl script, it happens so quickly that it looks like you edited in place, but you didn't.

Here's an example of what this might look like:

Code:
#!/usr/bin/env perl
use strict;
use warnings;

# Get everyone's names settled
my $old = 'foo.txt';
my $new = 'new.txt';
my $bak = $old . '.bak';

# Open everyone; check for errors
open my $in, '<', $old
  or die "Can't open $old for reading: $!";
open my $out, '>', $new
  or die "Can't open $new for writing: $!";

# First print new lines to the new file since we want these lines on the top
print $out "\n";
print $out "That's an empty line above this one.\n";
print $out "\n";
print $out "There's another.\n";

# Now copy all the lines of $old to $new - below the new lines
while (my $line = <$in>) {
  print $out $line;
}

# Close everyone and check for any errors
close $in  or die "Can't close $old: $!";
close $out or die "Can't close $new: $!";

# Rename so that $old is $bak and $new is $old
rename $old, $bak or die "Failed to rename $old to $bak: $!"; 
rename $new, $old or die "Failed to rename $new to $old: $!";
 
  


Reply


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: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
Adding users from file using PERL? happy13 Programming 3 12-22-2007 07:26 PM
Adding a line to file mikz Linux - General 2 03-03-2005 12:35 PM
adding new line at end of file airikah Programming 3 11-08-2003 04:27 AM


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

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