perl: replace multiple word on a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: replace multiple word on a line
# 1  
Old 07-07-2011
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
# 2  
Old 07-07-2011
Code:
sed 's/( MA "vertical" )/( BC "horizontal" )/' input_file

# 3  
Old 07-07-2011
thanks for quick respond.

I forgot to mention that the whitespace between the words MA and "vertical" could be different from line to line.
I am looking for a perl sample.
# 4  
Old 07-07-2011
CPU & Memory

Try this
Code:
perl -pi -e 's/\( MA.*\"vertical\" \)/( BC "horizontal" )/g' filename

# 5  
Old 07-07-2011
Let me describe the problem in different way:

I want to process a file which have multiple lines as below:

( word1 "word2" )
(worda "wordb")
...

I want to convert word1/worda - to $newWord1 and word2/wordb->to $newWord2 and the rest chars including the white space still keep the same on each line.

A sample Perl code for convert one line is good enough like:
$line = ( word1 "word2" )
the result will be
$line = ( newWord1 "newWord2" )

Thanks,
--Michael
# 6  
Old 07-08-2011
Let's say your data file looks like this:

Code:
$
$ cat data
( MA "vertical" )
( Unix     "Windows" )
( MA     "Boston" )
$
$

And you want to perform the following conversions:

Code:
MA       => BC
vertical => horizontal
Unix     => Apple
Windows  => Microsoft
Boston   => Victoria

You could put all that information as key->value pairs in a Perl hash. Then loop through the file, search for keys using regex and replace those keys by their corresponding values from the hash.

The Perl one-liner and its test run follow:

Code:
$
$ # contents of the data file
$ cat data
( MA "vertical" )
( Unix     "Windows" )
( MA     "Boston" )
$
$ # Run the Perl one-liner on the data file
$ perl -lne 'BEGIN {%x = ( "MA"       => "BC",
                           "vertical" => "horizontal",
                           "Unix"     => "Apple",
                           "Windows"  => "Microsoft",
                           "Boston"   => "Victoria"
                         )}
             print "($1$x{$2}$3\"$x{$4}\"$5)" if /^\((\s+)(\w+)(\s+)"(\w+)"(\s+)\)$/
            ' data
( BC "horizontal" )
( Apple     "Microsoft" )
( BC     "Victoria" )
$
$
$

tyler_durden
# 7  
Old 07-08-2011
Thanks Tyler
Your code works for me.
Appreciate that.

--Michael
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 only First Word in Line

I have query to replace the first word in line using below code but its replace the middle word too sed -i 's/load /# LOAD/' /tmp/check.sql Query 1 : UPDATE accheadcon_data_last_upload SET last_upload_date = '2017-07-23' Replace to UPDATE accheadcon_data_last_up# LOAD SET... (1 Reply)
Discussion started by: kaushik02018
1 Replies

2. 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

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. UNIX for Advanced & Expert Users

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... (2 Replies)
Discussion started by: mgpatil31
2 Replies

5. 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

6. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

7. Shell Programming and Scripting

Replace only if the keyword is the first word in every line

How do I replace only if the keyword is at the begining of a line? Code: -- a = “This is a print statement” print a -- What if I want to replace print by #print only in the second line i.e only if the line starts with that keyword. Please help me out. I'm new to SED. -----Post... (5 Replies)
Discussion started by: alexzubin
5 Replies

8. Shell Programming and Scripting

replace only 1st word of a line if it comes in the subsequent lines at same postion.

I have a file like this.. Maharastra Mumbai worli Maharastra Mumbai navy maharatra Pune Maharastra Nagpur Karnataka Bangalore Karnataka Mysore Karnataka Mangalore Punjab Amritsar punjab Jalandar my expected outcome should be like this Maharastra Mumbai worli ---------- ... (9 Replies)
Discussion started by: geeko
9 Replies

9. 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

10. Shell Programming and Scripting

How to replace a word at a parcitular line

Could someone tell me how to replace a word at a particular line by a single SED or AWK command? e.g. I have a file with the contents below: $ cat file1 111 AAA 333 CCC 222 BBB 444 CCC I want to replace the word "CCC" with a blank to get the desired output below: 111 AAA 333 CCC... (3 Replies)
Discussion started by: stevefox
3 Replies
Login or Register to Ask a Question