Shell Script [Replacing string]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script [Replacing string]
# 1  
Old 08-29-2011
Shell Script [Replacing string]

My friend and I are working on a project together and we are trying to create a shell script that renames, replaces a string in a text file. The newly created file with the substitution would replace the name of the original file. And the original file would be saved as "____.txt.bak"

This is the code we came up with:
PHP Code:
#!/bin/sh

if [ -"$3" ] ; then
mv 
"$3" "$3.bak"
sed "s/$1/$2/g" "$3.bak" "$3"
else
echo $
3 not found
exit 1
fi 
To test it, we created a file called happy.txt and in that file, there are only four words: happy happy cow happy


we did: ~/test/subst1 cow happy happy.txt


We thought that this would replace cow with happy, but it didn't work, we got this in return as a message:
PHP Code:
~/test/subst1testargument expected 




Any help would be appreciated...
# 2  
Old 08-29-2011
Try:
Code:
if [ -f "$3" ] ; then

# 3  
Old 08-29-2011
Hey thanks for that, it worked Smilie

but there's another problem now...we are trying now to create a second script called "subst2" that does the exact same thing as the "subst1" but leaves the file completely unchanged (and does not create the .bak file) if the string being replaced is nowhere in the file.

Also, the subst2 is supposed to be generalized so that it will produce a new script called "subst" that will apply a substitution to any amount of files...for example:

PHP Code:
~/test/subst happy cow myFile1.txt myFile2.txt myFile3.txt 
I figured that this might work:
PHP Code:
#!/bin/sh

if [ -"$3" ] ; then 
echo expect string1string2file
exit 1
fi 
if [ -"$3" ] ; then
mv 
"$3" "$3.bak"
sed "s/$1/$2/g" "$3.bak" "$3"
else
echo $
3 not found
exit 1
fi 

But it didn't...I think that -z is a problem...but really frustrated that I can't find why...my book doesn't help much Smilie
# 4  
Old 08-29-2011
In what way did it not work?

I'd do something like this:

Code:
if [ "$#" -lt 3 ]
then
        echo "usage:  a b file1 file2 ..."
        exit
fi

A=$1
shift
B=$1
shift
while [ "$#" -gt 0 ]
do
        echo "substitute $A for $B in $1"
        shift
done

# 5  
Old 08-29-2011
Thanks for the code above, but I'm afraid my knowledge of shell script does not extend that far Smilie...I do not know how to use the while command or done. I'm vaguely familiar with shift as well...

When I inputted:
PHP Code:
~/test/subst2 cat cow happy.txt 
Even though there was no string "cat" in the file "happy.txt", it still created a .bak file...I am trying to make it so that if there is no string in the file that I am looking for to replace, then nothing will happen to the file.

Also there was a mistake in my code, the actual code in my "subst2" file is this:

PHP Code:
#!/bin/sh

if [ -"$3" ] ; then 
echo expect string1string2file
exit 1
fi 
if [ -"$3" ] ; then
mv 
"$3" "$3.bak"
sed "s/$1/$2/g" "$3.bak" "$3"
else
echo $
3 not found
exit 1
fi 
# 6  
Old 08-29-2011
Quote:
Originally Posted by jzhang172

PHP Code:
#!/bin/sh

if [ -"$3" ] ; then 
echo expect string1string2file
exit 1
fi 
if [ -"$3" ] ; then
mv 
"$3" "$3.bak"
sed "s/$1/$2/g" "$3.bak" "$3"
else
echo $
3 not found
exit 1
fi 
the sed command is always going to execute if the value for $3 is a file. it will run a replace, which doesn't actually replace anything because the pattern doesn't match.

You need to add a check to see if the pattern passed in as $1 is found in the file before you run the sed and create the backup file

Code:
#!/bin/sh

if [ -z "$3" ] ; then 
echo expect string1, string2, file
exit 1
fi 
if [ -f "$3" -a "`/bin/grep "$1" "$3"`" ]; then
mv "$3" "$3.bak"
sed "s/$1/$2/g" "$3.bak" > "$3"
else
echo $3 not found
exit 1
fi


Last edited by declanryan; 08-29-2011 at 09:18 PM..
# 7  
Old 08-29-2011
Ok, so I actually tried a different approach and it works, your code works great too Smilie

PHP Code:
#!/bin/bash
if grep "$1" "$3" > /dev/nullthen
mv 
$$3.bak
sed 
"s/$1/$2/g" $3.bak > $3
fi 
However, i can't find a way now to generalize my subst2 script, which would produce a new script "subst" that will apply a substitution to any number of files given on the command line.

For example...~/test/subst happy cow myFile1.txt myFile2.txt
myFile3.txt


should apply the same substitution throughout each of the three files named there.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

2. Shell Programming and Scripting

Replacing number between xml tags with ksh shell script

Hallo, im basically a complete noob on shell scripting and im trying to replace or rather add 1 to a number between xml tags. The xml basically has a tag somewhere that looks like this: <tag>12345678901234</tag> Now i want to replace the number between the tags. And i want the file to... (6 Replies)
Discussion started by: Demoric
6 Replies

3. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

4. Shell Programming and Scripting

Replacing string with special characters in shell

Hi, I am trying to replace a string in shell but it is not working correctly. @xcom.file@ needs to be replaced with tb137 Plz help.Thx. Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: manish72
4 Replies

5. Shell Programming and Scripting

Replacing stripped off leading zeros in shell script

I have a script which is taking a 10 character variable (BOC) input by the user. If it begins with a zero, the script unwittingly strips that off, & passes a 9 characters variable. echo -n "Enter core-follow date/time for BOC: " setenv BOC $< The next bit of code picks up the 9... (4 Replies)
Discussion started by: wtaicken
4 Replies

6. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

7. UNIX for Dummies Questions & Answers

Replacing string

Hi there, I'd like to replace STRING_ZERO in FILE_ZERO.txt with the value of VALUEi-th by using something like that: VALUE1=1000 VALUE2=2000 VALUE3=3000 for((i=1;i<=3;i++)); do sed "s/STRING_ZERO/$VALUE'$i'/" FILE_ZERO.txt >> FILE_NEW.txt; done but it doesn't work... Any help... (9 Replies)
Discussion started by: Giordano Bruno
9 Replies

8. Shell Programming and Scripting

Writing script for finding a string and replacing

Hi all i need some help in writing a small script that searches a string and then replaces it by a new string for searching the best result i get is from find comand combined with xargs e.g. find . -name "*.*" |xargs grep -l "search string" this command I use in root directory and this... (4 Replies)
Discussion started by: Aditya_IT
4 Replies

9. Shell Programming and Scripting

replacing parameter in shell script

I have a script file as below named test1.sh sed -e 's/xxxkeys/$1/g' template1.asp > template1.txt sed -e 's/xxxkeys/$2/g' template2.asp > template2.txt sed -e 's/xxxkeys/$3/g' template3.asp > template3.txt sed -e 's/xxxkeys/$4/g' template4.asp > template4.txt I want to replace $1 with a,... (1 Reply)
Discussion started by: satgur
1 Replies

10. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies
Login or Register to Ask a Question