LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2010, 11:49 PM   #1
adarshmca
LQ Newbie
 
Registered: Dec 2010
Posts: 28

Rep: Reputation: 0
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
 
Old 12-28-2010, 12:02 AM   #2
kaushalpatel1982
Member
 
Registered: Aug 2007
Location: INDIA
Distribution: CentOS, RHEL, Fedora, Debian, Ubuntu, LinuxMint, PCLinuxOS
Posts: 153

Rep: Reputation: 9
# 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
 
Old 12-28-2010, 12:22 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
You could also write a bash script to loop through the file and then change the necessary item(s)
 
Old 12-28-2010, 12:35 AM   #4
adarshmca
LQ Newbie
 
Registered: Dec 2010
Posts: 28

Original Poster
Rep: Reputation: 0
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
 
Old 12-28-2010, 12:39 AM   #5
adarshmca
LQ Newbie
 
Registered: Dec 2010
Posts: 28

Original Poster
Rep: Reputation: 0
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
 
Old 12-28-2010, 02:13 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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.
 
Old 12-28-2010, 03:49 AM   #7
adarshmca
LQ Newbie
 
Registered: Dec 2010
Posts: 28

Original Poster
Rep: Reputation: 0
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
 
Old 12-28-2010, 04:20 AM   #8
tekin.frm
LQ Newbie
 
Registered: Jul 2010
Location: Ankara, Turkey
Distribution: openSUSE 11.1
Posts: 2

Rep: Reputation: 1
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.
 
Old 12-28-2010, 05:13 AM   #9
Helsvell
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
Thanks this has helped me alot.
 
Old 12-28-2010, 06:41 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find or replace through sed adarshmca Linux - Newbie 3 12-28-2010 12:35 AM
problems using sed to find and replace.... tdnnash25 Linux - Server 4 03-01-2010 07:34 PM
Use sed to find and replace a url xmrkite Linux - Software 4 10-10-2007 08:20 PM
sed - find and replace command bullshit Programming 9 01-05-2006 04:25 AM
SED;find and replace;help required gd13 Programming 3 12-21-2004 07:33 AM


All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration