how to insert text before first line in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to insert text before first line in perl
# 1  
Old 08-16-2006
how to insert text before first line in perl

Hello all
im doing simple parsing on text file , but now I need to insert
string before the first line of the text file , how can I do that in perl?
# 2  
Old 08-16-2006
Quote:
Originally Posted by umen
Hello all
im doing simple parsing on text file , but now I need to insert
string before the first line of the text file , how can I do that in perl?
That shouldn't be difficult... you need a temporary file. You read the file1.txt you write your string in file2.txt and after that you copy the content of file1.txt into file2.txt.
I think that Perl has some options for reading an entire text file into a variabile, but I'm not sure... if there is suchs function you won't need file2.txt.
# 3  
Old 08-16-2006
This will open the file, and parse it line by line. You mean something like this? ( Replace $0 with the name of the file you want to parse - filename must be in quotes. )

Code:
$ cat x.pl
#!/usr/bin/env perl

my $infile;

$infile = $0;

open( IN , "< $infile") or die "Can't open $infile. $!";

my $line;
my $i = 0;
while( $line = <IN> ) {
  $i++;
  chomp $line;

  printf STDOUT "string$i $line\n";
}
close(IN);

The result:
Code:
$ x.pl
string1 #!/usr/bin/env perl
string2
string3 my $infile;
string4
string5 $infile = $0;
string6
string7 open( IN , "< $infile") or die "Can't open $infile. $!";
string8
string9 my $line;
string10 my $i = 0;
string11 while( $line = <IN> ) {
string12   $i++;
string13   chomp $line;
string14
string15   printf STDOUT "string$i $line\n";
string16 }
string17 close(IN);
string18
string19

# 4  
Old 08-17-2006
Dear Umen,

You can simply use a flag value and put the line you want to write only for the first time.

regards
Apoorva Kumar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. UNIX for Dummies Questions & Answers

Insert a line in a text file

I want to insert a line with text after the 9th line of a text file. How would I do this using sed or awk? (2 Replies)
Discussion started by: lost.identity
2 Replies

4. Linux

How to insert new line in perl

HI, I have a text file in which I have removed all new lines as I would like to introduce a new line at the end of each record in the file. There is no common end line for all the records. A new record will start by *RECORD*. So I want to introduce a new line before this line *RECORD*. So Can... (2 Replies)
Discussion started by: kaav06
2 Replies

5. Shell Programming and Scripting

Perl:How to insert a line to a file.

Hi, Perl is new to me. I am trying to insert a line to a file. Example: I have a file (trial.txt), content: ZZZZ AAA DDDD I am trying to insert CCC below AAA. MY perl command: open (FILE,"+>>C:\\Documents and Settings\\trial.txt\n")|| die "can't open file"; while(<FILE>) { ... (6 Replies)
Discussion started by: SSGKT
6 Replies

6. Shell Programming and Scripting

sed insert text at particular line

I know that sed -n '12p' file will print line 12 but how might I insert text to a specified line? thanks (2 Replies)
Discussion started by: action_owl
2 Replies

7. Shell Programming and Scripting

Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />' GREP_FOR='</JOB>' TMP_FILE=/tmp/lineArray.$$ if ]; then continue else ... (7 Replies)
Discussion started by: J-Man
7 Replies

8. Shell Programming and Scripting

how to insert text between lines of an existing file using perl

Hi , I need some inputs on how to open a file (file.txt) and parse the text example aaa of the file and bbb of the file and add the text zzzz once i parse (aaa and bbb) and followed by the remaining of the text as it is in the file using perl programming. Thanks in advance (3 Replies)
Discussion started by: madhul2002
3 Replies

9. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

10. Shell Programming and Scripting

Insert text file at a certain line.

I need to insert a file called temp_impact (which has about 15 lines in it) to a file called 11.23cfg starting at line 33. I searched the forums and found the sed '34i\ test' 11.23cfg > newfile That will enter word test at the appropriate line, but i need the entire file dumped there. Any... (4 Replies)
Discussion started by: insania
4 Replies
Login or Register to Ask a Question