sed in a while loop with special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed in a while loop with special characters
# 1  
Old 03-05-2014
sed in a while loop with special characters

I have the foolowing data file:
File1
Code:
     <p name="A">5004</p>
      <p name="B">5004</p>
      <p name="C">5004</p>
      <p name="A">15004</p>
      <p name="B">15004</p>
      <p name="C">15004</p>

In a while loop using sed (100 of line need to be replace), I need the output to File3:
Code:
     <p name="A">5006</p>
      <p name="B">5004</p>
      <p name="C">5004</p>
      <p name="A">25006</p>
      <p name="B">15004</p>
      <p name="C">15004</p>

File2 contain:
Code:
"A">5004<	"A">5006<
"A">15004<	"A">15006<

Test Command:
Code:
sed -r 's/\"A">5004/\"A">5006/g' File2  <<<<<work

Code:
#!/bin/ksh
cat File2 | while read a b
do
sed -r "s/$a/$b/g" File2 > File3
cp File3 File2 
done


Please help!Smilie

Last edited by Franklin52; 03-05-2014 at 10:42 AM.. Reason: Please use code tags
# 2  
Old 03-05-2014
You don't need that while loop and it's not clear where you're using file1.
You should be doing something like this

Code:
sed -e 's/<stringa>/<stringb>/g' File2 > File3

The following string seems incorrect since you aren't quoting the second "...
Code:
's/\"A">5004/\"A">5006/g'

should be
Code:
's/\"A\">5004/\"A\">5006/g'

So at a guess you should be doing something like...
Code:
sed -e 's/\"A\">5004/\"A\">5006/g' File2 > File3

# 3  
Old 03-05-2014
Hi,
I'm not sure all understand, but try this:
Code:
$ cat file1
   <p name="A">5004</p>
      <p name="B">5004</p>
      <p name="C">5004</p>
      <p name="A">15004</p>
      <p name="B">15004</p>
      <p name="C">15004</p>

Code:
$ cat file2
"A">5004<       "A">5006<
"A">15004<      "A">15006<

Code:
$ printf "s/%s/%s/g\n" $(<file2) | sed -f - file1
   <p name="A">5006</p>
      <p name="B">5004</p>
      <p name="C">5004</p>
      <p name="A">15006</p>
      <p name="B">15004</p>
      <p name="C">15004</p>

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ignore special characters in loop

Hi All, select app from the menu: ABC DEF GHI JKL ALL # ALL will select all the apps in the menu echo "Enter your option" read option; if then <execute the below command> elif # option is the 1 selection from menu...not ALL <execute the below command> else echo wrong... (6 Replies)
Discussion started by: Devaraj A
6 Replies

2. Shell Programming and Scripting

Sed - remove special characters

Hi, I have a file with this line, it's always in the first line: I want to remove these special characters: ´╗┐ file1 ´╗┐\\bar\c$\test2\;3.348.118 Bytes;160 ;3 \\bar\c$\test\;35 Bytes;2 ;1 I want the same file to be only \\bar\c$\test2\;3.348.118 Bytes;160 ;3 \\bar\c$\test\;35... (4 Replies)
Discussion started by: nakaedu
4 Replies

3. Shell Programming and Scripting

sed special characters issue

Hi I'm trying to replace string1 by string2 in file homepage.htm as follows but is not working. Please Help: sedsed -i "s@'://your server name:port/test/owa'@'://11.22.33.44:5555/pls/SAMPLE'@g" homepage.htm Where, String1 ://your server name:port/test/owa String2... (3 Replies)
Discussion started by: koazter
3 Replies

4. Shell Programming and Scripting

Escape special characters in SED

Need help in escaping special characters in sed command. Here is the the string which i am trying to find a replace with From :- REQUEST_TYPE=PIXEL&amp;MSG_ID={//MESSAGE_ID} To :- REQUEST_TYPE=PIXEL&amp;MSG_ID= X_EDELIVERY_MESSAGE_ID &amp; BATCH_ID= X_EDELIVERY_BATCH_ID Here is the sed command i am... (2 Replies)
Discussion started by: aakishore
2 Replies

5. Shell Programming and Scripting

Need help with sed to escape special characters

Hello Everyone, I need to read an encrypted password from the user and update that value in an xml file. I am trying to use "sed" for searching the appropriate tag and replacing this new value that get from the user. Since the encrypted password can contain special characters(like /,\,&,etc),... (4 Replies)
Discussion started by: majose
4 Replies

6. Shell Programming and Scripting

SED with Special characters

Hello All Seeking the right one SED command. My attempt is: From orginal.txt by SED to target.txt sed -i "/('outbound-callerid/a\$ext->add($context, $exten, '', new ext_SipAddHeader('P-Preferred-Identity', '<sip:${CALLERID(nummer)}@carrier.com>'));" orginal.txtWhat am make wrong?:wall: ... (5 Replies)
Discussion started by: mdbinder
5 Replies

7. Shell Programming and Scripting

awk loop: display special characters

Hi everybody; I have a code and this fetches data from first.txt,modify it and outputs it to second.txt file. l awk 'NR>1 {print "l ./gcsw "$1" lt all lset Data="$2" Value "$3}' /home/gcsw/first.txt > /home/gcsw/second.txt this outputs as: l ./gcsw 123 lt all lset Data=456 Value 789 ... (1 Reply)
Discussion started by: gc_sw
1 Replies

8. Shell Programming and Scripting

sed with many special characters

I started with this: counter1=1 cp file.txt file_${counter1}.tmp while read name1 do echo $name1 counter2=`expr $counter1 + 1` sed /'${name1}'/d file_${counter1}.txt > file_${counter2}.txt counter1=`expr $counter1 + 1` done < source.txtsource.txt contains the... (1 Reply)
Discussion started by: lakanino
1 Replies

9. Shell Programming and Scripting

sed with special characters

Hi, I am reading a file (GC_JAR.log) which has entries like: 511725.629, 0.1122672 secs] 525268.975, 0.1240036 secs] 527181.835, 0.2068215 secs] 527914.287, 0.2884801 secs] 528457.134, 0.2548725 secs] I want to replace all the entries of "secs]" with just "secs" Thus, the output... (4 Replies)
Discussion started by: itzz.me
4 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question