LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find or replace without using sed (https://www.linuxquestions.org/questions/linux-newbie-8/find-or-replace-without-using-sed-852732/)

adarshmca 12-27-2010 10:49 PM

find or replace without using sed
 
Hi all,

I am working on writing a script for find and replace a string using sed.

But know I want to write without using sed.

Is it possible and someone Please help me write this code.


Thanks & Regards

Adarsh Sharma

kaushalpatel1982 12-27-2010 11:02 PM

# substitute (find and replace) "foo" with "bar" on each line
awk '{sub(/foo/,"bar");print}' # replaces only 1st instance
gawk '{$0=gensub(/foo/,"bar",4);print}' # replaces only 4th instance
awk '{gsub(/foo/,"bar");print}' # replaces ALL instances in a line


more examples on http://www.catonmat.net/blog/wp-cont...9/awk1line.txt

grail 12-27-2010 11:22 PM

You could also write a bash script to loop through the file and then change the necessary item(s)

adarshmca 12-27-2010 11:35 PM

Thanks but I am using a user input variable and file name to replace.

Here is my command sed command :

In sed we have to quote in " rather than ` to accept variables
sed "s/$findstring/$replacestring/g" $filename > /root/Desktop/output1.txt

Your command is

awk '{gsub(/foo/,"bar");print}' # replaces ALL instances in a line

But we have to mention filename, findstring variable and replace string variable.


Thanks

adarshmca 12-27-2010 11:39 PM

Hi grail , I also want the same thing.

First Loop through file with each word.
If condition to match
and code to replace.

Thanks & Regards

Adarsh

grail 12-28-2010 01:13 AM

Quote:

But we have to mention filename, findstring variable and replace string variable.
Easy enough:
Code:

awk -vfind=$findstring -vreplace=$replacestring 'gsub(find,replace)' $filename > /root/Desktop/output1.txt
I notice from this you are doing it as root. I would caution against this as root can clobber whatever he feels like.

adarshmca 12-28-2010 02:49 AM

Yes It works.

Thanks for your kindness.

But i didn't work as i expected.I expect to have changes in original file filename.txt but I checked, it displays only replacements i.e findstring replacestring in new file and also I checked with below command :

awk -vfind=$findstring -vreplace=$replacestring 'gsub(find,replace)' $filename

It displays

[root@ws-test Desktop]# bash adarsh2.sh find2.txt adarsh sharma
sharma
sharma
sharma
sharma
sharma
Script executed successfully

I want changes in original file.



Best Regards

Adarsh Sharma

tekin.frm 12-28-2010 03:20 AM

You can use a temp file.

awk -vfind=$findstring -vreplace=$replacestring 'gsub(find,replace)' $filename >$filename.tmp
mv $filename.tmp $filename




And it is my first post. :D

Helsvell 12-28-2010 04:13 AM

Thanks this has helped me alot.

grail 12-28-2010 05:41 AM

Quote:

I want changes in original file.
Whilst I see others have provided this solution for you, it was not part of the original request. You will see that I cut-n-paste directly from your own post #4 from the information
at the end of your sed command where you were not replacing the original file.

As has been indicated, awk does not have the '-i' option of sed to perform the change in place so you require a temp file and move.


All times are GMT -5. The time now is 08:33 AM.