Problem with sed (search/replace)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with sed (search/replace)
# 1  
Old 08-26-2008
Problem with sed (search/replace)

Hi,

In a file FILE, the following lines appear :

Code:
WORD 8 8 8
ANOTHERWORD blabla
...

Directly in the prompt, if I type

Code:
$sed '/WORD/s/8/10/g' FILE

it replace the 8's by 10's in file :

Code:
$cat FILE
WORD 10 10 10
ANOTHERWORD blabla
...

This is exactly what I want to do. Then, if instead in the prompt, I define

Code:
$before=8
$increment=2

and if I type

Code:
$sed '/WORD/s/'$before'/'$[$before + $increment]'/g' FILE

It also work perfectly (8's replaced by 10's).

Now, if instead in a script, the following line appear in a while loop

Code:
max=12
increment=2
before=10
while [ $before -le $max ]
do
   sed -i_temp '/WORD/s/'$before'/'$[$before + $increment]'/g'  FILE
   before=$[$before + $increment]
done

And if I run the script, it doesn't work! It replace "8 8 8" by "10" instead of "10 10 10"

What is my problem?

Thanks a lot for your help,

Tipi
# 2  
Old 08-26-2008
I forgot to say that max, before and increment are in fact arrays. So, when I call, $max, $increment, or $before, I call the element [0] of these arrays.

Maybe this can be the source of the problem? Instead of
max=12
increment=2
before=10
in the last post, you should read
max=( 12 12 12 )
increment=( 2 2 2 )
before=( 10 10 10 )

everything else stay the same.
# 3  
Old 08-26-2008
This doesn't work for me :
Quote:
$sed '/WORD/s/'$before'/'$[$before + $increment]'/g' FILE
If using variables in sed then use double quotes. Try this :
Quote:
sed "/WORD/s/$before/$[$before + $increment]/g" FILE
# 4  
Old 08-26-2008
Replace single quotes by double quotes in your sed command (variables will be expanded) and use (( )) for calculations
Code:
max=12
increment=2
before=10
while [ $before -le $max ]
do
   sed -i_temp "/WORD/s/$before/$((before + increment))/g"  FILE
   (( before = $before + $increment ))
done

Jean-Pierre.

Last edited by aigles; 08-26-2008 at 05:49 PM.. Reason: Quoting corrections
# 5  
Old 08-26-2008
My script is really huge so, right after the sed line, I added

echo "before+increment=$[$before+$increment]" >> REPORTFILE
echo "Wrote : `grep WORD FILE`" >> REPORTFILE

When I look in the REPORTFILE file, I can read :

before+increment=12
Wrote : WORD 12

So
WORD 10 10 10
really became
WORD 12

This is the same with single quote like in my fisrt post, or with double quoted sed :

sed -i_temp "/WORD/s/$before/$[$before + $increment]/g" FILE

Thanks a lot for your help,

Tipi
# 6  
Old 08-26-2008
Quote:
Originally Posted by aigles
Replace single quotes by double quotes in your sed command (variables will be expanded) and use (( )) for calculations
Code:
max=12
increment=2
before=10
while [ $before -le $max ]
do
   sed -i_temp "/WORD/s/'$before'/'$((before + increment))"/g'  FILE
   (( before = $before + $increment ))
done

Jean-Pierre.
Are the last " and ' to be interchanged? Because I have an open quote error...
# 7  
Old 08-26-2008
Run ur script using sh -x and check what is the sed command that is getting executed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Large search replace using sed results in memory problem.

I have one big file of size 9GB (big_file.txt). This big file has sentences and paragraphs like any usual English document. I have another file consisting of replacement strings for sed to use. The file name is replace.sed and each entry in one line looks like this: s/\<shout\>/shout/g s/\<b is... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

2. Shell Programming and Scripting

Need help with search and replace using SED

Hi guys, thanks for accepting me in your forum .. I am trying to clean some hacked PHP files using SSH .. I am using this command: find . -type f -print0 | xargs -0 sed -i '/god_mod/d' <?php ... (3 Replies)
Discussion started by: wisam74us
3 Replies

3. UNIX for Dummies Questions & Answers

search and replace with sed

Hi All I have a simple text file and I want to be able to replace any alpha character and comma combination with any other symbol of my choice here is the text file I want to replace: pear apple ban,ana grape ",g1234," te,st1 here is how it should look afterwards: pear... (4 Replies)
Discussion started by: greg_b
4 Replies

4. Shell Programming and Scripting

Sed - search and replace help.

Hi everyone, basically I am been cleaning data by using simple sed commands So what i have below has been working for me. sed 's/="//g' trade.csv > tradeb.csv sed 's/"//g' tradeb.csv > trade2.csv but now i don't want to remove all the quotes just the ones if i encounter this ... (1 Reply)
Discussion started by: raz0r
1 Replies

5. UNIX for Dummies Questions & Answers

How to use 'sed' to search and replace?

Hello - I have a very large file in which a certain numbers are repeated. I find that using vi to edit the entire file is useless. How should i use sed to find a replace such as this text: To replace: 145.D25.D558 With: 215.22.45.DW I tried this command: sed... (4 Replies)
Discussion started by: DallasT
4 Replies

6. Shell Programming and Scripting

sed search and replace

hi, im new for sed, anyone can help me to these in sed command my output file.txt "aaa",a1,bbb "ddd",a1,ccc "eee",a1,www need to change a1, to "a1"," output i need "aaa","a1","bbb "ddd","a1","ccc "eee","a1","www thanks in advance fsp (2 Replies)
Discussion started by: fspalero
2 Replies

7. UNIX and Linux Applications

GNU sed - Search and Replace problem

Hi, The following code loops through every file with an error extension and then loops through all XML files in that directory and replaces the target character @ with / . The problem I have is that if there is more than one occurance of @ in each individual file it doesn't replace it. Any... (2 Replies)
Discussion started by: Fishn
2 Replies

8. UNIX for Dummies Questions & Answers

Search/Replace with Sed

Is there a way to use the sed command to 1) search a specified pattern 2) in the line where that pattern is found, replace from character N to character N+4 with a new 4-character string. Thks in advance! (5 Replies)
Discussion started by: mvalonso
5 Replies

9. UNIX for Dummies Questions & Answers

sed search and replace

Hello Folks, Anyone know how I can replace this line in file.xml <oacore_nprocs oa_var="s_oacore_nprocs">8</oacore_nprocs> with this line <oacore_nprocs oa_var="s_oacore_nprocs">1</oacore_nprocs> using sed or awk ? Thanks for your time. Cheers, Dave (7 Replies)
Discussion started by: d__browne
7 Replies

10. Shell Programming and Scripting

Search and replace sed or tr

Hi folks, I need to search and replace specific text in a file and replace it. I have a text file that does not have any newlines or carriage returns. All newlines have been removed. Here is what I need to do. Find the exact string “DH” (quotes included) and replace it with \n”DH” (basically... (6 Replies)
Discussion started by: bridgeje
6 Replies
Login or Register to Ask a Question