Hi All,
If I have a line as following:
( MA "vertical" )
How can I convert it to as below:
( BC "horizontal" )
Thanks,
--Michael
Hi All,
If I have a line as following:
( MA "vertical" )
How can I convert it to as below:
( BC "horizontal" )
Thanks,
--Michael
sed 's/( MA "vertical" )/( BC "horizontal" )/' input_file
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.
Try this
perl -pi -e 's/\( MA.*\"vertical\" \)/( BC "horizontal" )/g' filename
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
Let's say your data file looks like this:
$
$ cat data
( MA "vertical" )
( Unix "Windows" )
( MA "Boston" )
$
$
And you want to perform the following conversions:
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:
$
$ # 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
Thanks Tyler
Your code works for me.
Appreciate that.
--Michael