Find and replace a string in a file using perl command from bash script
Hi,
I wanted to find and replace a string from a perl file. I have written a script in bash which runs the following command. perl -pi -e "s/$findstring/$replacestring/" testfile where as $findstring = print F_WC_TMP"$line\n"; and $replaceString = $line = join ' ', split ' ', $line; print F_WC_TMP"$line\n"; But when I am running the above command, i think it is replacing the $findstring with the above mentioned string and hence it contains a $line, it is looking for the variable $line and not finding the exact string. I am confused about how to search for a string that contains $ in it and replace it with another $string. Please help me on this |
Try to precede the $'s and the \'s with a \ (i.e. escape them), in both findstring and replacestring:
#!/bin/bash findstring='print F_WC_TMP"\$line\\n";' replacestring='\$line = join " ", split " ", \$line; print F_WC_TMP"\$line\\n";' perl -pi -e "s/$findstring/$replacestring/" testfile Risto |
If you understood you, you want a Perl script do you this. Here it is:
================================ PHP Code:
That should do it.. Good luck! |
Hello,
Thanks a lot. It worked. :) I have one more question. Actually I wanted to insert a new line before an already existing line. Thats why I am searching for the existing line and replacing it with a newline;existing line. But Is there any way just to insert a new line before an existing line without replacing it? Thanks. |
What if you insert a newline character into replacestring instead of space (after the first semicolon):
replacestring='\$line = join " ", split " ", \$line;\nprint F_WC_TMP"\$line\\n";' Risto |
Yes. It worked. Thanks a lot.
|
All times are GMT -5. The time now is 05:59 PM. |