Computing.Net > Forums > Unix > character replacement using sed/awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

character replacement using sed/awk

Reply to Message Icon

Name: Rohit
Date: September 11, 2004 at 19:53:44 Pacific
OS: UNIX
CPU/Ram: 1
Comment:


We have a tilde delimited file with tilde in the character data also. Character data is always in double quotes. For example

1~23~"abc"~1.1~"xy~x"

We need to change the tilde in character data to a dash, as below.

1~23~"abc"~1.1~"xy-x"

Can we do this using sed or a combination of sed and awk ?

Any help would be appreciated. Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: September 13, 2004 at 05:45:04 Pacific
Reply:

Heh! - I wouldn't bother sed or awk with this - they might feel insulted, get angry and blow up your computer. Better use tr '~' '-'


0

Response Number 2
Name: Wolfbone
Date: September 13, 2004 at 06:00:24 Pacific
Reply:

Doh! - You meant only the ~ between " and ":

sed 's/\(.*".*\)~\(.*\)/\1-\2/'

should work.


0

Response Number 3
Name: Wolfbone
Date: September 13, 2004 at 06:22:26 Pacific
Reply:

awk 'BEGIN {FS="\""; OFS="\""}; {for (i=2;i<=NF;i+=2) {sub("~","-",$i)}}; {print}'

if there's more than one substitution to be done in each line


0

Response Number 4
Name: Rohit
Date: September 13, 2004 at 19:56:35 Pacific
Reply:

Thanks Wolfbone!
Is "sub" a valid awk command on Unix ?

File does have multiple ~ that need to be delimited.

Thanks!


0

Response Number 5
Name: Wolfbone
Date: September 13, 2004 at 22:16:01 Pacific
Reply:

Is "sub" valid? - Not sure what you mean but it's not a GNU only extension. "gsub" is standard too, in case you also have more than one '~' in any field.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: character replacement using sed/awk

Awk and Sed - Line replacement ??? www.computing.net/answers/unix/awk-and-sed-line-replacement-/6041.html

sed/awk - line feeds? www.computing.net/answers/unix/sedawk-line-feeds/5237.html

sed/awk search and replace www.computing.net/answers/unix/sedawk-search-and-replace-/7872.html