LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   search for pattern in files and replace (https://www.linuxquestions.org/questions/linux-newbie-8/search-for-pattern-in-files-and-replace-131422/)

mizuki26 01-04-2004 11:07 AM

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

neo77777 01-04-2004 11:50 AM

man sed
enough said :D

TheOther1 01-04-2004 11:51 AM

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! :eek:

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!

TheOther1 01-04-2004 11:57 AM

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


All times are GMT -5. The time now is 04:32 PM.