Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-10-2009, 05:59 AM   #1
LQ Newbie
Registered: Jul 2009
Posts: 9

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

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 \"status red `date` `cat
/usr/local/hobbit/server/ext/history` \"";
print $RedAlertCommand,"\n";
my $GreenAlertCommand = system ("/usr/local/hobbit/client/bin/bb \"status green
`date` `cat /usr/local/hobbit/server/ext/history` \"");
system ("$GreenAlertCommand");
system ("$RedAlertCommand");
close Infile;

Please advise

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

Rep: Reputation: 233Reputation: 233Reputation: 233
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
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
LQ Newbie
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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 :
Old 08-12-2009, 06:35 AM   #5
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:

#!/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: $!";


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:31 AM.

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