Shell Script to replace text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to replace text
# 1  
Old 02-20-2010
Shell Script to replace text

I need a little help with a shell script. I want to be able to specify parameters so that the script searches multiple files (specified as parameters) and only modifies the file if it finds the string of text. For the files it doesn't find the string of text within, it should leave it alone.

So far, it seems like it only checks the first file and if that one is "true", it creates a .bak file for the remaining files (or vice versa if the first text file comes back as "false". It is replacing the text in all text files specified, but it is creating the .bak files when it is not necessary.

As an example, If i have four text files: text1.txt, text2.txt, text3.txt text4.txt, I would want to have the script look through each file, see if the string of text exists and then replace the string only if it exists, such as:

./script Hello Goodbye text1.txt text2.txt text3.txt text4.txt

Ideally, this would replace the string 'Hello' with the string 'Goodbye' in those four files.



Script I have so far:

Code:
#!/bin/sh

p1=$1
p2=$2
SEARCH=`grep $p1 $3 | wc -l`

while [ ! -z "$3" ]
do
  if [ $SEARCH -ge 1 ]
  then
    mv $3 $3.bak
    sed -e "s/$p1/$p2/g" $3.bak > $3
    shift
  else
    shift
  fi
done


Last edited by Scott; 02-20-2010 at 02:08 AM.. Reason: Please use code tags
# 2  
Old 02-20-2010
is this what you looking for?

Code:
for file in $(find . -type f -name "*.txt" -print)
do
		cp $file $file.bak
		sed 's/pattern1/pattern2/g' $file > $file.tmp
		mv $file.tmp $file
done

# 3  
Old 02-20-2010
Thanks for the reply.

The files to be replaced aren't necessarily .txt files. For example, one time running the script, I may want to replace a variety of .txt and .dat files, another time I may want to only want to replace .dat files or .txt files, etc.

The code I have now does replace the text successfully in all the files I specify. However, I want the files to not be touched/modified if grep does not find the string of text in the file.

---------- Post updated at 01:54 AM ---------- Previous update was at 01:37 AM ----------

I figured out what I was doing wrong. The $SEARCH variable shouldn't have been set as a global variable. It should have been within the "if" loop, such as:

Code:
#!/bin/sh

p1=$1
p2=$2

while [ ! -z "$3" ]
do
  SEARCH=`grep $p1 $3 | wc -l`
  if [ $SEARCH -ge 1 ]
  then
    mv $3 $3.bak
    sed -e "s/$p1/$p2/g" $3.bak > $3
    shift
  else
    shift
  fi
done


Last edited by Franklin52; 02-20-2010 at 07:43 AM.. Reason: Please use code tags!!
# 4  
Old 02-20-2010
Code:
  if [ $SEARCH -ge 1 ] ;
  then

i think you forgot to use semicolon just after paranthesis, try again please
# 5  
Old 02-20-2010
Code:
# test number of args
[ "$#" -lt 2 ] && exit 1

str="$1"
strnew="$2"
# destroy first args
shift 2

# loop rest args
for infile in $*
do
      # grep return exit <>0 if not true = continue = next file
      grep "$str"  "$infile"  >/dev/null 2>&1 || continue

      # include $str, replace, save original (.bak)
      bak="$infile.bak"
      mv "$infile" "$bak"
      sed "s/$str/$strnew/g" "$bak" > "$infile"
done

; need only if you have more than one command in the same line

Code:
if cmd ; then
   some
fi
# or
if cmd 
then
   some
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace stings in multiple text files

Good Evening Folks - Happy Friday! I have a need to replace a certain string in all .csv files from "0.00" to "#Missing" in my /app/hyp_app/files directory. Does anyone have a script they use regularly that's rather quick in performance? My files are rather large so I'm looking for a... (5 Replies)
Discussion started by: SIMMS7400
5 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

Bash shell: Replace a text block by another

Hello, I'm an starter in Bash scripting. I would like to write a script in Bash shell that replaces a specific text block (a function) by another text block in a file: for example in my file --> $HOME/myFile.js replacing following function between other functions in the file: function ABC()... (6 Replies)
Discussion started by: om1559
6 Replies

4. Shell Programming and Scripting

Shell or perl script to replace XML text in bulk

Hi, I am looking for assistance over shell or perl (without XML twig module) which replace string in XML file under particular branch..example of code file sample.. Exact requirment : Replace "Su saldo es" in below file with "Your balance" but only in XML branch of Text id=98 and Text Id=12... (7 Replies)
Discussion started by: Ashu_099
7 Replies

5. Shell Programming and Scripting

How to replace word with multiline text using shell scripting.

Hi all I have data files which contain data as shown below: Line 5: FIDE INST_DESC: DIAM Co Ltd/Japan => MAID Co Ltd/Japan INST_NME: DIAM Co Ltd/Japan => MAID Co Ltd/Japan Line 6: FIDE INST_DESC: DIAM DL/Pimco US Bond Open Born in the USA => MAID DL/Pimco US Bond Open Born in the... (6 Replies)
Discussion started by: Ganesh_more
6 Replies

6. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

7. Shell Programming and Scripting

gawk script to search and replace text in a flat file

Hi I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file. Input file text : ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 ZIDE_RECORD000... (5 Replies)
Discussion started by: gkausmel
5 Replies

8. Shell Programming and Scripting

Using shell to replace characters in a text file

Can I just say, this is such a frustrating and yet enormously rewarding field of study. I'm in the middle of configuring GeekTool (Uh oh, stupid n00b) and I really only have one question. I'm using Automator to grab a RSS feed, having GeekTool continually run that application every 10 minutes,... (7 Replies)
Discussion started by: SomeTechGuy
7 Replies

9. Shell Programming and Scripting

script - replace text in file

I have a file something like this (except much larger amount of rows/data) 0001 blue testing1 0002 blue testing2 0006 blue testing3 0232 red testing4 2143 blue testing5 3453 blue testing6 In a script I want to replace the word red with blue where the line begins with a... (4 Replies)
Discussion started by: frustrated1
4 Replies

10. UNIX for Dummies Questions & Answers

replace a value with another in shell script

hai everybody, i am new to scripting,i have to replace a value with another value ina script...both the values i have got in two seperate variables...can anyone give me the syntax how to do this in a shell script... ie. e1=$5,e2=$6 v2=$1,v3=$7 now i want to... (2 Replies)
Discussion started by: Babu
2 Replies
Login or Register to Ask a Question