How to add a line in a file using perl script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add a line in a file using perl script?
# 1  
Old 05-07-2012
How to add a line in a file using perl script?

Hi all,

I want to search for a line in a file using perl script and add a line above that line.Can any one suggest me command or code to that using script.

Thanks BHarath
# 2  
Old 05-07-2012
You are excepting this one

Code:
perl -p -i -e 's/raj/rani/g' test2.txt

# 3  
Old 05-07-2012
Code:
 
perl -lane 'if (/pattern/) {print "newword\npattern"}else{print $_}' filename.txt

# 4  
Old 05-07-2012
Or:
Code:
# cat infile
asa
sasaa
asasa
oooo
eee
eee

Code:
#perl -pi -e 'print "new line\n" if (/^ooo/);' infile

Code:
#cat infile
asa
sasaa
asasa
new line
oooo
eee
eee

# 5  
Old 05-07-2012
Hi

Actually my intention is to search a line which have one keyword for ex:-SLAVE,after searching i want add one line above this line.

Ex:- SLAVE"";---statement
now in this file i want to search for this word slave and above this line i want to add new line

//nes line added
SLAVE"";---statement

I want a perl script to do all this.
# 6  
Old 05-07-2012
ok.. and what's the problem with the suggestions above ?

Code:
#perl -pi -e 'print "new line\n" if (/SLAVE/);' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add a line in every file with perl commandline?

Hi, i want to add a line at the beginning of every file in a directory. perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1' *.reset But this command is updating only the first file in the directory. I think this is because $. is not resetting... (3 Replies)
Discussion started by: twistedpair
3 Replies

2. Shell Programming and Scripting

editing single line in html file in perl script

Hi Folks, It is regarding the perl scripting. I have an html file(many files) which contains the below line in the body tag. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> Now I want to read that html file through perl... (3 Replies)
Discussion started by: giridhar276
3 Replies

3. UNIX for Dummies Questions & Answers

Replace line via perl script of file

Hi All, I wanted to do following. 1) If file exit then open file for reading and check if my string is there then i wanted to replace the entire line with that string + something else then close the file. 2) If file does not exit then i need to open the file and write to it. I am done with... (0 Replies)
Discussion started by: Tarun24
0 Replies

4. Shell Programming and Scripting

Need awk script to add a prefix to each line in file

Hello , I have file with below content : '165567885', '165568443', '165568805', I need an awk script that would add a prefix zero after first ' . Like '0165567885', '0165568443', '0165568805', Please help. Thanks in advance. (5 Replies)
Discussion started by: rmv
5 Replies

5. Shell Programming and Scripting

awk, perl Script for processing a single line text file

I need a script to process a huge single line text file: The sample of the text is: "forward_inline_item": "Inline", "options_region_Australia": "Australia", "server_event_err_msg": "There was an error attempting to save", "Token": "Yes", "family": "Family","pwd_login_tab": "Enter Your... (1 Reply)
Discussion started by: hmsadiq
1 Replies

6. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

7. Shell Programming and Scripting

perl script to add a line into a file

hi all need a perl script to add a line into a file thanks with anticipation (2 Replies)
Discussion started by: karthikn7974
2 Replies

8. Shell Programming and Scripting

Reading each line of a file in perl script

HI I need to read each line (test.txt) and store it in a array (@test) How to do it in perl. Suppose i have a file test.txt. I have to read each line of the test.txt file and store it in a array @test. How to do it in perl. Regards Harikrishna (3 Replies)
Discussion started by: Harikrishna
3 Replies

9. Shell Programming and Scripting

script to add first line from another file in aix

Hi All, I want to add a header from one file to another file which is around 2GB in size currently i am using cat to do it. It took more time to process. Is there any way to do it? Please help me .. Thanks in Advance Selva S (4 Replies)
Discussion started by: selvarhce
4 Replies

10. Shell Programming and Scripting

add to first line of file using perl

Hey guys, I have a text file full of data and all I want to do with the thing is add the file extension name to the first line of the file. Here is what I have so far. I can't remember how to append to the first line of the file without deleting the exsisting data. #!/usr/local/bin/perl ... (5 Replies)
Discussion started by: kingdbag
5 Replies
Login or Register to Ask a Question