How to write a new line to the end of the file in Perl?


 
Thread Tools Search this Thread
Top Forums Programming How to write a new line to the end of the file in Perl?
# 1  
Old 05-14-2010
How to write a new line to the end of the file in Perl?

i am very new to Perl. i am using Ubuntu. i have a string call $string that contains following words "new line". i also have a data file as follows.

Code:
djfibjbet
etitrbjijbtr rrge     rgjierjegjeri
jerijg
kijij  jijij

i want to write my new line to my data file as follows.
Code:
djfibjbet
etitrbjijbtr rrge     rgjierjegjeri
jerijg
kijij  jijij
new line


Here is my code.


Code:
#!/usr/bin/perl
sub io
{
# usage:
# @array  = io('read',$file)
# $string = io('read',$file)
# io('write',$file,\$string)
# io('write',$file,\@array)

my($bit,$file,$data) = @_;

 if($bit eq 'read')
 {
    open IO,"< $file" or die "Cannot open $file for input: $!\n";
    my @file = <IO>;
    close IO;
    return wantarray ? @file :  join '', @file;
 }

 if($bit eq 'write')
 {
 open  IO,"> $file" or die "Cannot open $file for output: $!\n";
 print IO  ref $data eq 'ARRAY' ? @$data : ref $data eq "SCALAR"? $$data : ' ';
 close IO;
 }

}
    $string =`cat test.dat | grep  test`;
    io('write','/home/run/test.log',\ $string);

issue is, new line will replace all my old lines in the data file. how can i overcome this issue? can someone please help? Smilie

Last edited by Scott; 05-14-2010 at 07:32 PM.. Reason: Code, logs, data, input, output should go in code tags...
# 2  
Old 05-14-2010
Probably you need to open the output file for append mode, but you opened it for write mode.

Try
Code:
open  IO,">> $file" or die "Cannot open $file for output: $!\n";

This User Gave Thanks to pseudocoder For This Post:
# 3  
Old 05-14-2010
u are a god, thanks

---------- Post updated at 04:07 PM ---------- Previous update was at 02:46 PM ----------

so if i want to change,
Code:
io('write','/home/run/test.log',\ $string);

to
Code:
io('write','/home/run/<$name>test.log',\ $string);

how do i do that?
In other words, lets say i have XXXXXtest.log files, such as pretest.log, aftertest.log, badtest.log. so i want to use a string call name, so i can pick which file to write in to depending on the situation. Could you please tell me how to do that?

Last edited by Scott; 05-14-2010 at 07:31 PM.. Reason: Code tags, please...
# 4  
Old 05-14-2010
Not sure if I got you, hope this is what you were searching:
Code:
    $string =`cat test.dat | grep  test`;
    $ofile='/home/run/pretest.log';
    io('write',$ofile,\ $string);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File write begin/end, recording time

Hello all, How can I find out the start and end of the writing file in the directory or recording time for writing file? I have a directory where small ~ 1*MB temporary files are written. After the end of the record, they are retrieved and erased. I can only find out that the files are... (4 Replies)
Discussion started by: mrAibo
4 Replies

2. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

4. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

5. Shell Programming and Scripting

[Perl] Write to a file

Hai #!user/bin/perl -------- -------- print("$line.......\n"); ------- ------ I want to write inside $line into a file like run.log How i can pleasw tel me Thanks kiran (4 Replies)
Discussion started by: kiran425
4 Replies

6. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

7. Shell Programming and Scripting

How to write a new entry at the beginning of a log file instead of at the end?

Hi Ladies and Gents, Explanation of my question with an example: Let's consider the script: backup_every_hour.sh #!/bin/bash rsync -auv $dir $backup_dir >> backup_every_hour_script.log Each time this script is called there will be a new entry at the end of the file... (1 Reply)
Discussion started by: freddie50
1 Replies

8. Programming

concatinate all lines from second line to end of line in perl

I have datafile like ~dta.yunm shhshsgggssssjsksjs sggsjshsjsjssss shshshhshshshs i wish to take only first line and all other lines in concatenated form in second line what should I do??? output like ~dta.yunm shhshsgggssssjsksjssggsjshsjsjssssshshshhshshshs please tell me how... (7 Replies)
Discussion started by: sujit_singh
7 Replies

9. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

10. UNIX for Dummies Questions & Answers

tar: write error unexpected end of file

Hi, I have attempted to backup some database files on my company's Solaris machine about 3 times now. Each time that I attempt the backup, about 6 files are evident on the dat tape when I do read of the media and then I see the dreaded "tar: write error unexpected EOF" message. Each time that I... (2 Replies)
Discussion started by: robyn
2 Replies
Login or Register to Ask a Question