Issues with string replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issues with string replacement
# 1  
Old 07-18-2009
Issues with string replacement

I posted this issue a week or so ago and I still cant get the script to work. Here is what I have so far and what Im trying to do.

Code:
#!/bin/ksh

old="$1"
new="$2"
file=$3
grep $1 $3 > /dev/null
if [ $? -eq 0 ];then
    cp $3 ${3}.bak
    sed "s/$1/$2/g" $3 > ${3}~
    mv ${3}~ ${3}
fi

the script is used to use parameters to replace strings in a file.

so if I have

~/direct/subst2 string1 string2 filename.txt

subst2 is the script and string 1 should be replaced by string 2 in the file. However I want to search the file for the string1 and if it contains it then replace it and make the backup file. If not, then dont make any changes and no backup file. For some reason the replacement is working correctly.

Last edited by vidyadhar85; 07-18-2009 at 02:55 AM.. Reason: code tags added
# 2  
Old 07-18-2009
Worked for me. Can you supply exact command line you are using and a sniplet of the file you are trying to change.
# 3  
Old 07-18-2009
I think there might be some space in one of your params.
Try this:
Code:
#!/bin/ksh
old="${1}"
new="${2}"
file=${3}

egrep -l "${1}" "${3}"  && mv "${3}" "${3}.bak"
if [ $? -eq 0 ];then
   sed "s/${1}/${2}/g" "${3}.bak" > "${3}"
fi

I have reduced someofyour codes.
# 4  
Old 07-20-2009
sweet, thank you I got it to work. Is there an easier way to do this, I have more than one file?

---------- Post updated at 02:04 AM ---------- Previous update was at 01:58 AM ----------

like is there a way for me to do


~/direct/subst2 string1 string2 filename.txt filename2.txt filename3.txt filename4.txt

etc.
# 5  
Old 07-20-2009
Hope you were looking for something like this

Code:
old="${1}"
new="${2}"
shift 2
files="$@"
grep -l $old $files|while read fname
do
   sed "s/$old/$new/g" $fname > $fname.tmp
   mv $fname.tmp $fname
done

# 6  
Old 07-20-2009
exactly what i was looking for, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parallel replacement string

Hi, Using the following command, I can only get rid of the last extension from my input file name: parallel command '>' {.}.output ::: my.input.file The output file is "my.input.output" How can I get rid of the last two extensions of my input file name, so that end up with "my.output"?... (4 Replies)
Discussion started by: forU
4 Replies

2. Shell Programming and Scripting

String replacement

Hi, I have a text file where all records come in one line (single line file), each record starts with 'BUCH' and ends with '@&' and if data is not there we get space instead. between '@&' and next record there might be some spaces, now I want to remove those spaces between '@&' and 'BUCH'. ... (4 Replies)
Discussion started by: maks475
4 Replies

3. Shell Programming and Scripting

String replacement.

Dear Friends, I want to replace following line with given line. It should grep/search following string in a file (input.txt) M/M SRNO: 000M/6200-0362498 COSMETIC PRO MALE FEMALE Once found it should replace it to following string. T_DLHNNO: 000M/6200-0362498 ... (7 Replies)
Discussion started by: anushree.a
7 Replies

4. Shell Programming and Scripting

String replacement

Hi All, I have below file which has data in below format. #$ | AB_100 | AB_300 ()| AB_4 @*(% | AB-789 i want o/p as below format. | AB_100 | AB_300 | AB_4 | AB-789 So here there is no standard format. How we can achieve the same in unix ? Regards, (3 Replies)
Discussion started by: gander_ss
3 Replies

5. Shell Programming and Scripting

String replacement using sed

I need to search and replace a particular string in a file. Only the exact match of the string should be replaced. eg: File contents : abc abcd abcdef --> Replace only 'abc' with 'xyz', but it should not replace abcd with xyzd. So the o/p should be: xyz abcd abcdef. How can this be done? I... (5 Replies)
Discussion started by: sngk
5 Replies

6. Shell Programming and Scripting

String replacement

I have one string string1=user/password:IP_ADDR:Directory I need to replace string1 value like store into string2 string2=user password:IP_ADDR:Directory i.e replace "/" character by '<space>' character But i wouldn't use any file in the meantime. Please help me......................... (6 Replies)
Discussion started by: mnmonu
6 Replies

7. Shell Programming and Scripting

String replacement

Hi I am new to shell scripting but i manage to do some simple things. I am trying to replace a string in one file. I am using sed to replace but it is not permanently writing to the file, rather it is temporary. I want to know whether is there any another method to replace a string in a file... (7 Replies)
Discussion started by: reddybs
7 Replies

8. Shell Programming and Scripting

Replacement of string

Hi I have a text file which contains the following. AAA,BBB,CCC,DDD AAA,BBB,CCC,DDD AAA,BBB,CCC,DDD How can I replace all CCC with 888, with other contents inside the file remain unchange? Please advice Desired output: AAA,BBB,888,DDD AAA,BBB,888,DDD AAA,BBB,888,DDD (1 Reply)
Discussion started by: c0384
1 Replies

9. Shell Programming and Scripting

String Replacement with Perl

I want to replace a string within a file using perl. We have a line that gets commented out, and I want to replace that line now matter how it was commented out. for example, I'd want to replace ###ES=PR1A with ES=PR1A or ##LJW(9/16/26)ES=PR1A with ES=PR1A I tried: perl... (4 Replies)
Discussion started by: Lindarella
4 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question