How to replace special chars like " ' " (Apostrophe)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to replace special chars like " ' " (Apostrophe)
# 1  
Old 10-26-2008
Question How to replace special chars like " ' " (Apostrophe)

I'm goin to drive crazy soon, if i can not do this. I have a text file (570kb) and i have to replace the apostrophe " ' " and minus "-" with space " ". i have done it for minus:

sed 's/-/ /g' aaa.txt >zzz.txt

this replaced minus with space.

but i can not use the same command for ' .

sed 's/'/ /g' aaa.txt >zzz.txt

does not work, Smilie i need help, i think Smilie
# 2  
Old 10-26-2008
Yo try this:
Code:
sed s/"'"/" "/g aaa.txt >zzz.txt

I just learnt something!

Last edited by dan-e; 10-26-2008 at 09:58 PM.. Reason: Update code to poster's code
# 3  
Old 10-26-2008
oh my gosh! Smilie i really learnt something new! i have googled 2 days, found nothin' .
but can you explain why, if possible?

thx dan-e Smilie
# 4  
Old 10-26-2008
.... haha I'm only reading this forum so that I can learn more myself, but I came across a similar example in a hardcopy book called "Sed & Awk" (O'reilly). According to the book you don't need to enclose your sed argument with quotes (as you have in this example - single quotes), and I'm sure this is what was breaking your statement.

I tried a few things myself and decided it was the outside quotes breaking things, and then found out you can enclose your pattern and substitution string with double quotes (not sure if singles work too). Gave it a shot and it worked.

The reason that your example fails is because these such commands rely on 'delimeter' characters to break up their data. Eg. for a program to read a string it finds the next quote - " - and then starts grabbing anything after the quote until it hits another quote - " - and then says 'everything in-between is the string'. Of course this doesn't work if the string itself contains a quote, like in your example. To get around this problem, they invented 'escape' characters which say to the program 'hey don't worry about this quote - it's part of the string' Eg:
1. "A string" = A string
2. "A string with \"escape characters\"" = A string with "escape characters"
3. "A broken string without "escape characters"" = A broken string without

Your example falls into category 3. However, I couldn't find a way to use an escape character with the single-quote. I would have thought that this would work:

Code:
sed 's/\'/ /g' aaa.txt >zzz.txt

- Note the \ escape character preceding the single-quote. But this doesn't work. I don't know why, if people would program like me it would work. Smilie But they don't. Which is why bad software exists. And microsoft.

Last edited by dan-e; 10-26-2008 at 10:46 PM.. Reason: Fixed ugliness
# 5  
Old 10-26-2008
oh, i think i need to buy some more books Smilie thx dan-e for the whole information.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

"set variable with spaces and apostrophe after s"

Hi Guys, I have a variable such that: set x = (Session,Date,Type,Receive Coil Name,Manufacturer,Manufacturer's Model Name) foreach i ($x) echo $i end I would like to read each variable one by one like: Session Date Type Receive Coil Name Manufacturer Manufacturer's Model Name Is... (1 Reply)
Discussion started by: dixits
1 Replies

3. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Delete chars after dot "."

Hi, I require to delete all characters after a dot "." from a string. For e.g. input_var="/home/dips/file_1_20100726.txt.gz /home/dips/file_2_20100726.txt.gz /home/dips/file_3_20100726.txt.gz" output_var="/home/dips/file_1_20100726 /home/dips/file_2_20100726... (3 Replies)
Discussion started by: dips_ag
3 Replies

6. Shell Programming and Scripting

How to replace "®" "™" "with Spaces in UNIX

do U know how to replace the registered trademark "®" symbol or trade Mark "™" "with Spaces in UNIX (4 Replies)
Discussion started by: MMeari
4 Replies

7. Shell Programming and Scripting

Question about special variables: "-" and "$_"

both ksh/bash support this 2 special variables, Is there any document for reference? 1) "-" is $OLDPWD 2) "$_" is last argument of previous command. (4 Replies)
Discussion started by: honglus
4 Replies

8. Shell Programming and Scripting

Removing " " chars using Awk

HI Friends, I am trying to elliminate the " " characters from the word: "hello" using awk. I need the output to be just = hello (without " " chars). Is there any way to do this ? Thanks! (3 Replies)
Discussion started by: vijaya2006
3 Replies
Login or Register to Ask a Question