Syntax error using sed for a find and replace - sed

I am trying to do a find and replace using sed. I am trying to find that : purge: [], to replace by purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"], but it does not work, here is my commande :
sed -i -e 's/purge: [],/purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],/g' myfile.txt
Could you help me please ?
Thank you very much !

You need to:
Change delimiters to a char other than a /, like ~ or !, or escape / delimiter chars inside the pattern (I'd suggest the former)
Escape the [ char in the pattern.
You can use
sed -i 's!purge: \[],!purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],!g' myfile.txt
See the online demo:
#!/bin/bash
s='Blah..purge: [], blah...'
sed 's!purge: \[],!purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],!g'<<< "$s"
# => Blah..purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"], blah...

Related

using sed to replace ' character with \'

I have a string that goes like this abcd'efgh\ ijkl\ mnop
I want to make the string into abcd\'efgh\ ijkl\ mnop
sed 's/\'/\\\'/g didn't work
You can use
sed 's/'"'"'/\\&/g'
See the online demo:
#!/bin/bash
s="abcd'"'efgh\ ijkl\ mnop'
sed 's/'"'"'/\\&/g' <<< "$s"
# => abcd\'efgh\ ijkl\ mnop

Sed replace strings starts with special characters

I'm trying to replace strings with sed in ; php_value[date.timezone] = Europe/Riga
i tried something like this:
sed -i 's/; php_value[date.timezone] = Europe/\Riga/; php_value[date.timezone] = America/\Sao_Paulo/g' file
Output:
sed: -e expression #1, char 47: extra characters after command
You can use
sed -i 's/; php_value\[date\.timezone] = Europe\/Riga/; php_value[date.timezone] = America\/Sao_Paulo/g' file
See the online demo.
NOTE:
[ and . are special regex metacharacters and need to be escaped to match literal [ and ., hence, \[ and \. in the regex part
/ is a regex delimiter char here, and should also be escaped. To escape /, use \/. Well, if you use another regex delimiter char, you will have no need escaping /, e.g.
sed -i 's,; php_value\[date\.timezone] = Europe/Riga,; php_value[date.timezone] = America/Sao_Paulo,g' file
See the commas as regex delimiters here.

How to replace # using sed c0mmand?

I have the following header :
#SRR1561197.1/1
#SRR1561197.2/1
#SRR1561197.3/1
#SRR1561197.4/1
I want to Add few letters after # and before SRR like this:
#MexD1SRR1561197.1/1
#MexD1SRR1561197.2/1
#MexD1SRR1561197.3/1
#MexD1SRR1561197.4/1
I tried:
sed 's/#/#MexD1/File,fastq > change.fastq
This results in empty file..
Use sed with the in file replacement option. The g at the end makes it global.
sed -i 's/#/#MexD1/g' file
To fix your code.
sed 's/#/#MexD1/g' File.fastq > change.fastq
You have to escape it: sed s/\#/\#MexD1/g source-file-name > change.fastq

sed substitute with quotes and wildcard

I need to replace if ($_SESSION['POST']['*']==1){ with if (isset($_SESSION['POST']['*']) && $_SESSION['POST']['*']==1){
(I'm using * as a wild card)
I've tried sed -i "s/if ($_SESSION['POST']['.*']/if (isset($_SESSION['POST']['.*']) && $_SESSION['POST']['.*']/g" *.php and a few other variations without success.
Here goes...
sed "s/\(if (\)\(\$_SESSION\['POST']\['\([^']*\)']\)==1/\1isset(\2) \&\& \$_SESSION['POST']['\3']==1/" file
Using double quotes means that the $ symbols must be escaped, otherwise they will be interpreted as shell variables. The square brackets need to be escaped, otherwise they will be interpreted as the beginning of a range. It's OK to leave the closing square brackets as they are.
In order to capture the key, I have used a character class [^']*. This means zero or more characters that are not a single quote.
In the replacement, the captured groups (the parts between parentheses in the match) are referred to using \1, \2, etc.
Testing it out:
$ cat file
if ($_SESSION['POST']['foo']==1){
// do something
}
if ($_SESSION['POST']['bar']==1){
// do something else
}
$ sed "s/\(if (\)\(\$_SESSION\['POST']\['\([^']*\)']\)==1/\1isset(\2) \&\& \$_SESSION['POST']['\3']==1/" file
if (isset($_SESSION['POST']['foo']) && $_SESSION['POST']['foo']==1){
// do something
}
if (isset($_SESSION['POST']['bar']) && $_SESSION['POST']['bar']==1){
// do something else
}
By the way it makes the command a few characters shorter if you use extended regexp mode (-r or -E). In extended mode, the parentheses enclosing capture groups don't have to be escaped but literal ones do, so your command would then be:
sed -r "s/(if \()(\$_SESSION\['POST']\['([^']*)'])==1/\1isset(\2) \&\& \$_SESSION['POST']['\3']==1/" file
This sed should work:
s="if (\$_SESSION['POST']['name']==1){"
sed -r 's/(if +)\((([^=]+)[^\)]+)/\1(isset(\3) \&\& \2/' <<< "$s"
if (isset($_SESSION['POST']['name']) && $_SESSION['POST']['name']==1){
PS: Use sed -E instead of sed -r on OSX.
Here's another.
This is what we need to produce:
Pattern: if (\$_SESSION\['POST'\]\['\([^']*\)'\]
Replacement: if (isset($_SESSION['POST']['\1']) \&\& $_SESSION['POST']['\1']
When quoted in shell level:
Pattern: "if (\\\$_SESSION\['POST'\]\['\([^']*\)'\]"
Replacement: "if (isset(\$_SESSION['POST']['\1']) \\&\\& \$_SESSION['POST']['\1']"
Putting it together:
sed -i "s|if (\\\$_SESSION\['POST'\]\['\([^']*\)'\]|if (isset(\$_SESSION['POST']['\1']) \\&\\& \$_SESSION['POST']['\1']|g" file
Test:
# sed "s|if (\\\$_SESSION\['POST'\]\['\([^']*\)'\]|if (isset(\$_SESSION['POST']['\1']) \\&\\& \$_SESSION['POST']['\1']|g" <<'EOF'
> if ($_SESSION['POST']['ABC']==1){
> EOF
if (isset($_SESSION['POST']['ABC']) && $_SESSION['POST']['ABC']==1){

How can replace this using sed?

I'm using OS X, want to replace
[self.lang getAppLanguageString:#"foo bar"]
to
LocalizedString(#"foo bar", nil)
I use sed like the below:
sed -i '' s/[self\.lang getAppLanguageString:#"([a-zA-Z]+)"]/LocalizedString(#"\1", nil)/g somefile
but not work, how can I do that?
You've already escaped . in the pattern, but also need to escape [ and ].
Try:
sed -i 's/\[self\.lang getAppLanguageString:#\("[^"]*"\)\]/LocalizedString(#\1, nil)/' somefile