How to Replace to Apostrophe ' Inside a File Using Sed

How to replace to apostrophe ' inside a file using SED

Use " instead. And add g flag to replace all.

sed  "s/\+/\'/g" test.txt

How do I replace single quotes with another character in sed?

Try to keep sed commands simple as much as possible.
Otherwise you'll get confused of what you'd written reading it later.

#!/bin/bash
sed "s/'/ /g" myfile.txt

Unable to delete apostrophe using SED command on text file

Rather than removing things you don't want, you may want to focus on what you do want.

grep -Po '([0-9]+\.[0-9]+,? ?)+' input_file > ~/Desktop/testgcloud/results.txt

This will write to results file contents matching the pattern: at least one number, decimal, at least one number and an optional comma and space. Seemingly solving your problem in only one step.

If you're really set on sed, the following will remove empty apostrophes and optional commas and spaces.

sed "s/'',\? \?//g"

Replacing text with apostrophe text via sed in applescript

Try:

set findList to "Thats.nice"
set replaceList to "That's nice"
set fileName to "Thats.nice.whatever"
set resultFile to do shell script "echo " & quoted form of fileName & " | sed \"s/Thats.nice/That\\'s nice/\""

or to stick to your example:

set findList to "Thats.nice"
set replaceList to "That's nice"

set fileName to "Thats.nice.whatever"
set resultFile to do shell script "echo " & quoted form of fileName & " | sed \"s/" & findList & "/" & replaceList & "/\""

Explanation:

The sed statement is usually enclosed by single quotes like this:

set myText to "Hello"
set xxx to do shell script "echo " & quoted form of myText & " | sed 's/ello/i/'"

However, in this example you could have exluded the single quotes altogether.

set myText to "Hello"
set xxx to do shell script "echo " & quoted form of myText & " | sed s/ello/i/"

The unquoted sed statement will break down as soon a space is included.

set myText to "Hello"
set xxx to do shell script "echo " & quoted form of myText & " | sed s/ello/i there/"
--> error "sed: 1: \"s/ello/i\": unterminated substitute in regular expression" number 1

Since you can't include an apostrophe within a single quoted statement (even if you escape it), you can enclose the sed statement in double quotes like this:

set myText to "Johns script"
set xxx to do shell script "echo " & quoted form of myText & " | sed \"s/ns/n's/\""

EDIT
Lauri Ranta makes a good point that if your find or replace string contains escaped double quotes my answer won't work. Her solution is as follows:

set findList to "John's"
set replaceList to "\"Lauri's\""
set fileName to "John's script"
set resultFile to do shell script "echo " & quoted form of fileName & " | sed s/" & quoted form of findList & "/" & quoted form of replaceList & "/"

sed replace single/double quoted text?

You can replace the single quotes in the sed command with double-quoted single quotes. The shell sees a single quote as ending a string. So, let it. You had

sed -i 's/'ADMIN_USERNAME','memcache'/'ADMIN_USERNAME','u'/g' /var/www/html/memcache.php

But, if you replace the ' in the sed command with '"'"', then shell will see the first ' as ending the first single-quoted string, then "'" as a double-quoted single quote, and then the last ' as a beginning of a new single-quoted string. That'd be

sed -i 's/'"'"'ADMIN_USERNAME'"'"','"'"'memcache'"'"'/'"'"'ADMIN_USERNAME'"'"','"'"'u'"'"'/g' /var/www/html/memcache.php

You should also be able to do '\'' in place of the ' within the command, for the same reason.

sed -i 's/'\''ADMIN_USERNAME\'',\''memcache\''/\''ADMIN_USERNAME\'',\''u\''/g' /var/www/html/memcache.php

But really, it'd be better to use an alternative mechanism. I'd suggest defining the source and target strings as variables, and then put those in the sed string.

SRC="'ADMIN_USERNAME','memcache'"
DST="'ADMIN_USERNAME','u'"
sed -i "s/$SRC/$DST/g" /var/www/html/memcache.php

That's way more readable, and it makes it easier for you to handle the quoting mess in a sane way with bite-sized chunks. Yay "shell variable contents aren't subject to word expansion unless you force it" knowledge. :)

Make sure you don't put a / in the $SRC or $DST variables, though. ;)

sed: backslash-apostrophe pattern replacement

This might work for you (GNU sed):

sed 's/\\'\''/\\ '\''/g' fileIn > fileOut

Or:

sed "s/\\\'/\\\ '/g" fileIn > fileOut

The first solution punches a hole into the shell and retrieves a quoted single quote, whereas the second surrounds the sed script by double quotes. In both cases back slashes need to be quoted by a back slash.



Related Topics



Leave a reply



Submit