LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   one liner needed for search and replace (https://www.linuxquestions.org/questions/linux-general-1/one-liner-needed-for-search-and-replace-31096/)

chr15t0 09-24-2002 09:11 AM

one liner needed for search and replace
 
Hi guys,

I need a quick oneliner - either perl or bash to search all files in the current directory for the expression "fred_flintstone" and replace it with "barny_rubble". I wrote a perl script to do this a few weeks ago, but it's at home on my laptop.. and I'm up to /here/ right now at work !

any help would be much appreciated.

thanks
christo

so far I have got to
$ find -type f | xargs /bin/sed s/fred_flintstone/barny_rubble/

but I can't work out how to redirec the output back into the file found!

chr15t0 09-24-2002 09:30 AM

$perl -pi -e 's/fred_flintstone/barney_rubble/' *

seems to do the trick!

Christo :)

unSpawn 09-24-2002 10:00 AM

If you like using Bash, then you better resort to a quick function or script. Sed is a stream editor, so you'll have to feed it input and get the output. Else there's a small tool called "rpl" (see freshmeat) I've been using for years to replace text, works like a charm.

I extended the script a wee bit because if it is a file (find) but doesn't include the term (grep -q) then we need not (cat) it.

#!/bin/sh
x="fred_flintstone"; y="barny_rubble"; find -type f | while read z; do grep "$z" -qie "$x"; case "$?" in 0) cat "$z" | sed -e "s/$x/$y/g" > "$z".tmp; mv -f "$z".tmp "$z";; esac; done


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