LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 10-27-2008, 06:48 AM   #1
shokeer
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
Find & Replace Command Linux


Hi All
i want to search & replace file with a certain text.
Example
i have a file called xx.txt include several lines
medo=1
sido=2
kiko=3
riko=4

and i want to search on kiko=3 & replace it with far=70

how can i do that.what is the right command to do that.

please help me.
 
Old 10-27-2008, 07:05 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
You want sed for this:
Code:
sed 's/kiko=3/far=70/g' xx.txt > output_file
For more information and examples, see http://www.brunolinux.com/02-The_Ter..._with_Sed.html.

Last edited by pwc101; 10-27-2008 at 07:07 AM.
 
Old 10-27-2008, 09:35 AM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,095
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
Use rpl, it'll let you do search & replace on groups of files, even recursively through a directory structure
 
Old 01-23-2012, 05:39 AM   #4
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
linux script

Hi All
i want to search & replace certain text from a file usin script code not directly with sed command or any other command.i mean using while,for and if statement i want to replace text from a text file.
Example
i have a file called xx.txt include several lines:

hello xyz
my name is xyz
change the name xyz to abc

and i want to search on xyz & replace it with abc

how can i do that?what is the right script(usin while, for and if) to do that.
Please help me out.

Last edited by amit4465; 01-23-2012 at 05:40 AM.
 
Old 01-23-2012, 05:47 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,413

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Using find command you can do it like this way:

Code:
#find /home/xx.txt -type f -exec sed -i 's/kiko=3/far=70/g' {} \;
 
Old 01-23-2012, 01:25 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I've found that Google is a really good resource for learning about ways to do things on Linux. Try putting "find and replace text Linux" into the search bar, for example, and see what it turns up.

The LQ search feature is also pretty good.
 
Old 01-23-2012, 07:43 PM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 569

Rep: Reputation: 42
This works:

sed 's/xyz/abc/g' $file > /tmp/tempfile.tmp && mv -f /tmp/tempfile.tmp $file

I'd advise you to read up on sed command,it really helps
 
Old 01-23-2012, 08:43 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
As you can see from the above responses, you need to be very clear to state what you want to accomplish. If you give an example, you will get many proposed solutions for that specific example. If you have a general problem, start by explaining it in its full generality, and then provide several examples.

For example, if you were looking for a Linux equivalent to the MS "Search" function, the grep command can be used to list all files that contain a specific string - or any regular expression (described in the grep documentation, and elsewhere).

Or, as a few responses above illustrated, you can use the stream editor (sed) to identify the files containing some string (or regular expression) and modify the file's contents based on that finding. (You can even use the -i option to the sed command to modify the file "in place" without creating a copy of the file.) If you have more complex needs, you can write scripts for sed to run instead of including the script on the command line.

If sed doesn't solve your problem, the gawk computer language can be used to handle very complex problems of text manipulation.

If you have the pinfo command installed, and the associated documentation, pinfo <command> can often be used to display extensive documentation for "<command>." For example, pinfo gawk will display a hyper-linked book about gawk, which is often helpful. (The is an older command, info, that does much of the same things as pinfo, but it's not as easy to use.)
 
Old 01-24-2012, 12:49 AM   #9
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Hi Satyaveer

Quote:
Originally Posted by Satyaveer Arya View Post
Using find command you can do it like this way:

Code:
#find /home/xx.txt -type f -exec sed -i 's/kiko=3/far=70/g' {} \;
Hi,
I know sed command and what all command we have, but i want to run the script using while, for and if loop. Instead of using sed command or any other command. SED command and many other commands came later in linux but before it was only the coding using logic. So i want a script without using any executable commands.
I just want to use while, for, if statements to be used in the script. I hope u got my point now. so my friend please send me a script without using any command, just use simple logic(while, for, if).

Example: Text File: abc.txt contains;

Hi jkl
how r u?
change the name jkl to xyz.
*********************************
Now i want to replace jkl in the above text file with xyz. And the script can be run only once, we can't run it for the second time, because once the script's run jkl will be replaced by xyz in the original text file-> abc.txt.
**********************************
So next time when we open abc.txt we can see:-

Hi xyz
how r u?
change the name xyz to xyz.
**********************************

Hi All
i want to search & replace certain text from a file usin script code not directly with sed command or any other command.i mean using while,for and if statement i want to replace text from a text file.
Example
i have a file called xx.txt include several lines:

hello xyz
my name is xyz
change the name xyz to abc

and i want to search on xyz & replace it with abc

how can i do that?what is the right script(usin while, for and if) to do that.
Please help me out.

Last edited by amit4465; 01-24-2012 at 12:51 AM.
 
Old 01-24-2012, 12:54 AM   #10
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Replacing words from a text file

Hi All,
I know sed command and what all command we have, but i want to run the script using while, for and if loop. Instead of using sed command or any other command. SED command and many other commands came later in linux but before it was only the coding using logic. So i want a script without using any executable commands.
I just want to use while, for, if statements to be used in the script. I hope u got my point now.So please send me a script without using any command, just use simple logic(while, for, if).

Example: Text File: abc.txt contains;

Hi jkl
how r u?
change the name jkl to xyz.
*********************************
Now i want to replace jkl in the above text file with xyz. And the script can be run only once, we can't run it for the second time, because once the script's run jkl will be replaced by xyz in the original text file-> abc.txt.
**********************************
So next time when we open abc.txt we can see:-

Hi xyz
how r u?
change the name xyz to xyz.
**********************************

Hi All
i want to search & replace certain text from a file usin script code not directly with sed command or any other command.i mean using while,for and if statement i want to replace text from a text file.
Example
i have a file called xx.txt include several lines:

hello xyz
my name is xyz
change the name xyz to abc

and i want to search on xyz & replace it with abc

how can i do that?what is the right script(usin while, for and if) to do that.
Please help me out.
 
Old 01-24-2012, 01:08 AM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Bookmark & read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Between them they cover almost all of bash except maybe some very latest options.
 
Old 01-24-2012, 01:40 AM   #12
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,413

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
This code will help you in finding and replacing the text in a file:

Quote:
#!/bin/bash
OLD="xyz"
NEW="abc"
DPATH="/home/you/foo/*.txt"
BPATH="/home/you/bakup/foo"
TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
if [ -f $f -a -r $f ]; then
/bin/cp -f $f $BPATH
sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
else
echo "Error: Cannot read $f"
fi
done
/bin/rm $TFILE
 
Old 01-24-2012, 01:56 AM   #13
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Hi Satyaveer

again u r using sed command in the script....i want without using sed command, only with while,for,if.

Please help me out.

---------- Post added 01-24-12 at 12:27 PM ----------

Quote:
Originally Posted by Satyaveer Arya View Post
This code will help you in finding and replacing the text in a file:
again u r using sed command in the script....i want without using sed command, only with while,for,if.

Please help me out.
 
Old 01-24-2012, 05:03 AM   #14
chidam
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Posts: 4

Rep: Reputation: Disabled
Thumbs up

This will help you.

a.txt file contains

xyz
xxyyxxzz
xyzzz
xy
xyz

Shell program :-

#!/bin/bash
OLD="xyz"
NEW="abc"
F1PATH="a.txt"
F2PATH="b.txt"
while read line
do
if [ "$line" = "$OLD" ]; then
line=$NEW
fi
echo $line >> $F2PATH
done < $F1PATH
mv $F2PATH $F1PATH


After execution of shell script:-

abc
xxyyxxzz
xyzzz
xy
abc
 
Old 01-24-2012, 03:04 PM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
If you want to replace "xyx" by "abc," you can try this: (based on chidam's code, above.)

Code:
#!/bin/bash
OLD="xyz"
#Or, to replace all occurrences of xyz by abc, use OLD="/xyz"
NEW="abc"
F1PATH="a.txt"
F2PATH="b.txt"
while read line
do
  line=${line/${OLD}/${NEW}}
  echo $line >> $F2PATH
  done < $F1PATH
mv -f $F2PATH $F1PATH
Note: Untested code. (I had to use a MS operating system for a while.)

Note 2: This is bash specific. the Bourne shell does not, I believe, support the search-and-replace parameter expansion.

Note 3: Is this a homework problem? Your requirements sound like something an instructor might think would be a useful exercise. If so, please review the LQ policy on homework.
 
  


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
Can I use GREP to find & replace text? jim.thornton Linux - Newbie 3 07-18-2008 07:36 PM
sed - find and replace command bullshit Programming 9 01-05-2006 04:25 AM
Find & Replace Carriage Return in ooo linuxian Linux - Software 1 04-09-2005 06:43 PM
Find & Replace Benr Linux - Newbie 5 05-03-2004 03:37 PM
find and replace in files from command line dexter_modem Linux - General 4 06-10-2003 12:27 PM


All times are GMT -5. The time now is 09:03 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration