Register a domain and help support LQ
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-30-2006, 05:30 AM   #1
LQ Newbie
Registered: May 2006
Posts: 3

Rep: Reputation: 0
Unhappy search & replace

I want to search & replace following string

'/etc/passwd |grep "^molis:"'


'/etc/passwd |grep "^wam:"'

in the entire directly

some please help in this

thank you
Old 05-30-2006, 06:27 AM   #2
spirit receiver
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Did you mean 'in each file in the current directory'? If so, the following should do:
for i in $( grep -l '/etc/passwd |grep "\^molis:"' * ); do sed -i.bak -e 's=/etc/passwd |grep "^molis:"=/etc/passwd |grep "^wam:"=g' $i; done
This will not only do your search & replace, but also create a backup of each changed file with the extension ".bak". But make sure that this won't overwrite any existing files you'd like to keep.
Old 05-30-2006, 06:38 AM   #3
Senior Member
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
There are multiple text editors available in Linux. Most people have their favorite editor or two. One (of probably several) editors that understand regular expressions is vi (or vim). Commands can be given to do the replacement in part or all of the file. If you use an intermediate, temporary file, you can also do what you want non-interactively using sed, a stream oriented editor. Check out the man pages ("man vi" or "man sed").

Since passwd is a system file, you will need to be running as root (superuser). If you are using the shadow passsword file (and you should be), you will need to modify that file too.
Old 05-30-2006, 07:00 AM   #4
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
I wasn't sure whether you wanted to include the quotes? (') I included them in this example:

sed -i.bak 's#\('"'"'/etc/passwd\ |grep\ "^\)molis\(:"'"'"'\)#\1wam\2#g' *
(copy and paste this, mind the quotes!)
This sed finds all strings
<everything>'/etc/passwd |grep "^molis:"'<everything>
and replaces.
It makes a backup of the original files because of the .bak option.
Without backups it would be -i
I you need more explanation of this command, post so. Cya!
Old 10-05-2006, 02:05 PM   #5
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
sed -i.bak 's/molis/wam/g' /etc/passwd


replace, search

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Best search & replace program geokker Linux - Software 3 09-28-2005 03:41 PM
How to "Search & Replace" in html files using Perl? rebel Red Hat 8 04-09-2005 12:58 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

All times are GMT -5. The time now is 09:33 PM.

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