How to change only one occurance of apostrophe with sed


 
Thread Tools Search this Thread
Top Forums Programming How to change only one occurance of apostrophe with sed
# 1  
Old 06-15-2011
How to change only one occurance of apostrophe with sed

Hi,

I have a document with usual English text and some of the words have apostrophes (e.g. don't, can't, etc.)

I would like all these apostrophes to be doubled (e.g. don''t, can''t, etc.), but the problem is, that some of such words have double apostrophe and

by using sed -i "s/'/''/g" it would end up with for apostrophes (e.g. don''''t, etc.)

For now I have a work around that first I change one to two apostrophes and then four to two, but I would like to do it in a clean way.

Thanks for suggestions.

Pavel
# 2  
Old 06-15-2011
Code:
 
bash-3.00$ sed "s/\'/&&/g" /tmp/myfile
don''t
dont''t
bash-3.00$ cat /tmp/myfile
don't
dont't

# 3  
Old 06-15-2011
Quote:
sed "s/\([^']\)'\([^']\)/\1''\2/g"
Here the regex matches only string <non-quote><quote><non-quote>.

It would have been more appropriate to post this question in 'scripting' forum.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find ' (apostrophe) by grep

hi linux expert how can i find apostrophe (') symbol in text file by grep? for example for find 'test, the command grep " \'test" * is not useful. Many Thanks samad (1 Reply)
Discussion started by: abdossamad2003
1 Replies

2. UNIX for Beginners Questions & Answers

Usage of sed, position of apostrophe

Hello, I have never ever seen below notation for string substitution. sed -i -e 's/tttt/pppp'/g /var/bin/czech.sh; The strange thing for me is the position of the apostrophe. Should it be before the / or after the g? If I had been writing that command line, I would have chosen below way:... (3 Replies)
Discussion started by: baris35
3 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

5. Shell Programming and Scripting

Remove apostrophe and a letter followed by it

Hi All, For example, I have some words like these: cabinet's school's field's you'll I know how to remove apostrophe from these words using this command: cat filename | tr -s "\'" ' ' But I am not sure how I can remove the letter followed by the apostrophe, so that my output... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

Replace apostrophe with backslash apostrophe

I'm coding using BASH and have a requirement to replace apostrophes with backslash apostrophes. For example below: I am here 'in my room' ok Would be changed to: I am here /'in my room/' ok The original text is coming from a field in a MySql database and is being used by another process that... (5 Replies)
Discussion started by: dbjock
5 Replies

7. Shell Programming and Scripting

SED: Extracting text between first occurance of foo in front of bar

Suppose I have a text file that contains the tags <foo> and <bar>. The text file can have unlimted occurances of <foo> and <bar> and looks somthing like this: <foo> Some Text <foo> Some Text <bar> Some Text <foo> Some (1 Reply)
Discussion started by: ArterialTool
1 Replies

8. Shell Programming and Scripting

Sed command to globally replace xth occurance.

This should replace the 1st and the 3rd occurance of "the": sed -e "s/ the / those /1;s/ the / these /3" This works only by line. If there is a second "the", but in an other line for example, it counts it as 1st again and replaces it again. How can I replace "the" 1st and 3rd... (14 Replies)
Discussion started by: lowmaster
14 Replies

9. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

10. Shell Programming and Scripting

How to strip apostrophe from a file

I am trying to remove or replace various extraneous characters from a file so that subsequent processes work correctly. The characters that is giving me trouble is the apostrophe '. The command I 'm trying is sed 's/\'//g' ${IN_WRK_DIR}/file1 > ${IN_WRK_DIR}/file2 in a Korn script on HP... (8 Replies)
Discussion started by: aquimby
8 Replies
Login or Register to Ask a Question