Bash script to replace all occurrences of a string in all files

30/12/07

Permalink 10:05:59 pm, by alt3rn4tiv3 Email , 98 words,
Categories: Hacks, Cool Stuff

Bash script to replace all occurrences of a string in all files

Christina has kindly alerted me that Jobbie is a bad word, especially vulgar in Scotland. But the word was littered everywhere in the language files.

So, I wrote a Bash script (blatantly ripped / modified from where credit is due), if anyone’s interested -

Code:

#!/bin/bash
OLDSTRING="Jobbie"
NEWSTRING="Jobster"
DPATH="/home/username/directory/*.txt"
TFILE="/tmp/out.tmp.$$"
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    sed "s/$OLDSTRING/$NEWSTRING/g" "$f" > $TFILE && mv $TFILE "$f"
  fi
done
/bin/rm $TFILE

Edit -
rhican’s code:
sed -i "s/orig/new/g" *

Trackback address for this post:

http://altblog.searix.net/comtrack/trackback.php/72

Comments, Trackbacks, Pingbacks:

Comment from: Asby [Visitor] Email
Actually that is a shell script, perl would be easier to use:

$ cd /home/username/directory/
$ perl -i -pe 's/Jobbie/Jobster/g' *.txt
PermalinkPermalink 12/31/07 @ 20:36

This post has 1 feedback awaiting moderation...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))

Alt3rn4tiv3's Tech Blog


Like my blog?

January 2009
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Search

Categories

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 7

Google Ads

Other Ads

PayPerPost

Hire Me Direct

Valid XHTML 1.0 Transitional

Valid CSS!

Add to Technorati Favorites

feedNuts Feed Profile

powered by b2evolution free blog software