The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:06 AM
special character on Filename.. help!!!urgent genzbeat HP-UX 1 01-18-2008 03:55 PM
convert special character like £ cynnie Shell Programming and Scripting 1 08-08-2007 07:37 AM
Special character in my file Ryan2786 UNIX for Dummies Questions & Answers 3 07-06-2007 12:35 AM
special character ? mile1982 High Level Programming 1 10-19-2004 09:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-23-2008
MizzGail's Avatar
MizzGail MizzGail is offline
Registered User
  
 

Join Date: Sep 2001
Location: Syracuse, NY
Posts: 164
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
  #2 (permalink)  
Old 01-23-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,764
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
  #3 (permalink)  
Old 01-23-2008
MizzGail's Avatar
MizzGail MizzGail is offline
Registered User
  
 

Join Date: Sep 2001
Location: Syracuse, NY
Posts: 164
HM. That sounds like on the right track, but its not working.
  #4 (permalink)  
Old 01-23-2008
MizzGail's Avatar
MizzGail MizzGail is offline
Registered User
  
 

Join Date: Sep 2001
Location: Syracuse, NY
Posts: 164
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0