LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-04-2004, 12:07 PM   #1
mizuki26
LQ Newbie
 
Registered: Nov 2003
Distribution: Madrake 9.1
Posts: 8

Rep: Reputation: 0
search for pattern in files and replace


hi,

i am looking for a bash command for searching through files and replacing a specific pattern. anybody knows how to solve this?

thanx,
mizuki
 
Old 01-04-2004, 12:50 PM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 55
man sed
enough said
 
Old 01-04-2004, 12:51 PM   #3
TheOther1
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335

Rep: Reputation: 32
Try man sed for everything (almost) you wanted to know about sed (Serial EDitor). You want something like this:

sed -e s/what_U_R_looking_4/what_U_want_it_2_B/ file1 > file2

This says:
run sed
-e means evaluate this expression
s/x/y/ means search & replace (where x = a regex or pattern to be changed and y = a regex or pattern you want to end up with)
file1 is input file
file2 is output file

You can do this inplace but if you screw up, your original file is gone!

If you need more info on regexes, see man 7 regex or you can go to The Regex Coach and download the "coach" to learn more about regexes and how to use them.

PERL will be a much more eloquent solution and is probably already installed on your system. It's the "swiss-army knife" of programming languages and is very well suited to text manipulation. See perl.org for info on PERL. If it is not installed on your system, see Active State for distributions for just about any platform.

HTH!
 
Old 01-04-2004, 12:57 PM   #4
TheOther1
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335

Rep: Reputation: 32
If you want to do a bunch opf files at once, try this:
for i in /some/dir/filespec; do sed -e s/regex/regex/ $i > $i.new; done

Your new files will be named orignal_name.new
 
  


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
search/replace in many files allelopath Linux - General 1 08-02-2005 10:21 PM
Search and Replace a string1 with string2 in many files TroelsSmit Linux - Newbie 5 12-16-2004 02:04 PM
Search and Replace over multiple files The_Nerd Linux - Software 8 06-20-2004 07:59 AM
simple search and replace accross files/direcotries podollb Linux - Software 3 06-04-2004 09:30 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


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

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