Util for replacing text in files

Discuss Applications
Post Reply
User avatar
Basher52
guru
guru
Posts: 928
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Util for replacing text in files

Post by Basher52 »

Is there a utility that can replace text in files?

Something like 'grep', it can't matter where in the file the the text is and the the searched text should be replaced with what I write as replacement.
It also don't matter if it has to be used with other thing like 'grep' to find the files and pipe that to another program to do the actual replacement.

Anyone get what I need?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

There are many utils that will do this (including vim, sed, etc). I actually wrote a little utility that I use all the time that does a search replace on a single file or multiple files:

It's just a simple little bash script that calls perl:

http://voidmain.is-a-geek.net/files/scripts/sr

User avatar
Basher52
guru
guru
Posts: 928
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Post by Basher52 »

oh yeah, forgot that
I need it to be able to run it as s script

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

My script I just linked to should work for you then. The instructions are in the comments.

User avatar
Basher52
guru
guru
Posts: 928
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Post by Basher52 »

I'll try that :) thx man :D

User avatar
Basher52
guru
guru
Posts: 928
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Post by Basher52 »

Is it possible to replace more then just one word?
like sr 'aa bb cc' 'dd ee ff'? or is it only one word only?

By using more than one word I might lower the risk of replacing the wrong things.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

It's not really a word but a string so yes. I would suggest making a copy of the file you want to change and then experiment.

User avatar
Basher52
guru
guru
Posts: 928
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Post by Basher52 »

yeah, off course I make a backup... well the first time but if it works then why would I, I trust you :)

But I didn't get your answer, can you use '' to make the "word" more than just one word. or haven't you just tried it so you can't say?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

I guess I don't know what your asking. It works with a single source string and a single replacement string. That string can contain more than one word. For example:

$ sr " a rat" " an elephant" animals.html

would change the 6 character string " a rat" to the 12 character string " an elephant" wherever that string of characters appears in the file animals.html. It's pretty much that simple. If you want to change 2 different strings run it twice, each time with the different strings you want to change.

User avatar
Basher52
guru
guru
Posts: 928
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Post by Basher52 »

nono your example just gave me the answer that you CAN use " to distinguish words =)
I thought it was the character ' but you says it is the character "

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

I didn't say anything more than what is in the comments at the top of the program like I said. :) It's a "string" of "characters". "word" really doesn't have a meaning with this program. :)

Code: Select all

#!/bin/bash
######################################################################
# Program: sr
# Programmer: Void Main
# Syntax: sr oldtext newtext file(s)
# Search and replace text in files
#
# Example 1:
# $ cd /var/www/html
# $ sr ".kicks-ass.net" ".is-a-geek.net" *.html
#
# Example 2:
# $ cd /var/www/html
# $ find -name '*.html' | xargs sr ".kicks-ass.net" ".is-a-geek.net"
#
# Make sure you have good backups in case you change more
# than you intended. For example if you want to change "tick"
# to "tack" it would also change "ticket" to "tacket" so you
# need to think a little before running.
######################################################################

ARGC=$#
OLD="$1"
NEW="$2"
FILE="$3"
shift
shift

[ $ARGC -ge 3 -a -f "$FILE" ] && 
   perl -i -p -e "s/$OLD/$NEW/g;" $* ||
   echo "Syntax: sr oldtext newtext file(s)
Search and replace text in files"

Post Reply