Perl command to replace word in file...


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Perl command to replace word in file...
# 1  
Old 02-19-2013
Perl command to replace word in file...

Hi,

I want to search for a specific word in file and replace whole line with new text.

e.g.
1) I have file with below lines
APP=ABCD 12/12/2012
DB=DDB 01/01/2013

I need perl command which will check for APP=$VAL and replace whole line with APP=$NEWVAL $NEWDT

Simlarly need a command which will help for below.
"ABCD 12/12/2012"
"ABCDE 12/01/2013"
I have to search ABCD and replace only that line.

Thanks in advance.
# 2  
Old 02-19-2013
It's like sed in a bottle: Perl tutorial: Substitution and translation
Code:
s/^APP=[^ ]* [01]*[0-9]\/[0-3]*[0-9]\/[21][09][0-9][0-9] *$/APP=$NEWVAL $NEWDT/
 
s/^ABCD [01]*[0-9]\/[0-3]*[0-9]\/[21][09][0-9][0-9] *$/.../

Since it is PERL not sed, you need a loop that reads lines into string variables for substitution.
# 3  
Old 02-19-2013
Code:
cat file

APP=ABCD 12/12/2012
DB=DDB 01/01/2013

perl -pe '$NEWVAL="DCBA";$NEWDT="01/01/2013";$_ =~ s/ABCD\s12\/12\/2012/$NEWVAL $NEWDT/' file

APP=DCBA 01/01/2013
DB=DDB 01/01/2013

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace exact word by perl

my inputfile: This is a test and only a test for production - prod. echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g' outputfile: This is a test and only a test for testuction - test I only want to replace prod, not production. I also... (3 Replies)
Discussion started by: loktamann
3 Replies

2. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

3. Shell Programming and Scripting

perl: replace multiple word on a line

Hi All, If I have a line as following: ( MA "vertical" ) How can I convert it to as below: ( BC "horizontal" ) Thanks, --Michael (6 Replies)
Discussion started by: mxn731
6 Replies

4. Shell Programming and Scripting

Perl Parse word from command output

Hello, I used the following script to conect to cisco router: #!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$'); my $device =... (5 Replies)
Discussion started by: ahmed_zaher
5 Replies

5. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

6. Shell Programming and Scripting

Replace aword in a.The replaced word should not be overwitten in perl(details inside)

Hi i am trying to write a perl program where i have to open a 1)directory "unit" 2) rename the files in the dir say file1.txt;file2.txt...file5.txt to file1_a.txt;file2_a.txt,....file5_a.txt ;file1_x.txt ;file2_x.txt 3) open these renamed files and replace the words lets say file1_a.txt... (7 Replies)
Discussion started by: madhul2002
7 Replies

7. UNIX for Dummies Questions & Answers

how to use sed or perl command to find and replace a directory in a file

how to use sed command to find and replace a directory i have a file.. which contains lot of paths ... for eg.. file contains.. /usr/kk/rr/12345/1 /usr/kk/rr/12345/2 /usr/kk/rr/12345/3 /usr/kk/rr/12345/4 /usr/kk/rr/12345/5 /usr/kk/rr/12345/6 /usr/kk/rr/12345/7... (1 Reply)
Discussion started by: wip_vasikaran
1 Replies

8. UNIX for Dummies Questions & Answers

Replace word in a file

Guys, I need to replace a portion of the lines in the file depending on the word in my file i have this content use DBNAME go print "use DBNAME" i want to replace the variable DBNAME with something else. But the catch is this line is not always "use DBNAME". Sometimes it can be like "use... (3 Replies)
Discussion started by: sasiharitha
3 Replies

9. Shell Programming and Scripting

replace word in a file

there are 300 files in a directory , some of these files has a word "error" , I word to change this word to "message" , can advise what can i do ? thx. (5 Replies)
Discussion started by: ust
5 Replies

10. Shell Programming and Scripting

Perl script assistance; paste word into external command

I'm attempting to create a Perl script that will: Take the contents of the usernames.tmp file (usernames.tmp is created from an awk one-liner ran against /etc/passwd) Take one line at a time and pass it to the su command as a users name. This should go on until there is no more name to... (10 Replies)
Discussion started by: bru
10 Replies
Login or Register to Ask a Question