The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed-Special character replacement usshell Shell Programming and Scripting 3 05-22-2008 07:06 AM
special character on Filename.. help!!!urgent genzbeat HP-UX 1 01-18-2008 12:55 PM
convert special character like £ cynnie Shell Programming and Scripting 1 08-08-2007 03:37 AM
Special character in my file Ryan2786 UNIX for Dummies Questions & Answers 3 07-05-2007 08:35 PM
special character ? mile1982 High Level Programming 1 10-19-2004 05:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-23-2008
MizzGail's Avatar
Registered User
 

Join Date: Sep 2001
Location: Syracuse, NY
Posts: 158
sed and special character in data

I have a script that is reading an existing report, pulling out the customer code, then tacking on the customer name from another file and replacing the existing customer code with the new field. This was written for me by someone else. I'm not real familiar with sed.

The data is getting into the variables correctly, but if the customer name contains a "&" sign, then it repeats the first variable.


My statements are

y=`echo $a$b$i`
z=`echo $a$b$i$b$x`
sed s/"$y "/"$z "/g a.txt > CusT_TmP
cat CusT_TmP a.txt

When I run the script I get back
a=CUST-CD:
b=" "
i=123456
x= W & B

y=CUST-CD: 123456
z=CUST-CD: 123456 W & B Corp

But the entry in the output comes out like

CUST-CD 123456 W CUST-CD 123456 B Corp

Any customer name that I have an & in the name does this.

Any ideas of how to fix this?
Thanks
Reply With Quote
Forum Sponsor
  #2  
Old 01-23-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
sed treats & as the pattern space - in other words what sed found as a match for your regular expression. & as an operator means "print the pattern space".

One workaround without doing a lot of sed hacking is to change the & character to something else, then change it back using tr:
Code:
tr '&' '+' < a.txt > tmpfile
y=`echo $a$b$i`
z=`echo $a$b$i$b$x`
sed s/"$y "/"$z "/g tmpfile | tr '+' '&' > CusT_TmP
cat CusT_TmP a.txt
Reply With Quote
  #3  
Old 01-23-2008
MizzGail's Avatar
Registered User
 

Join Date: Sep 2001
Location: Syracuse, NY
Posts: 158
HM. That sounds like on the right track, but its not working.
Reply With Quote
  #4  
Old 01-23-2008
MizzGail's Avatar
Registered User
 

Join Date: Sep 2001
Location: Syracuse, NY
Posts: 158
I used that idea against the customer name file and just left it like that into the output file.
The I re-translated the output file back and it works!
Thanks!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0