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
Merging Two Files by a Single Column in Unix
Difference Between Number in the Same Column Using Awk
Convert a Text File into Columns
Timestamp Accuracy on Ext4 (Sub Millsecond)
How to Initiate Array Element to 0 in Bash
Git Unable to Create File Permission Denied
How to Add a Footer to the Bottom of Each Page of a Postscript or PDF File in Linux
How to Change a Certain Field of a File into Upper-Case Using Awk
Detecting Command Not Found in Bash Script
How to Use Local and Remote Variables Within a Heredoc or Command Over Ssh
Apache Mod_Rewrite Not Working with .Htaccess File
Sed - Piping a String Before the Last Line in a File
How to Use Stdin with Caption in Imagemagick
Why File Is Accessible After Deleting in Unix
What's the Point of Using Busybox in a Low Ram Embedded System