Sed with Special Characters

sed with special characters


sed 's/\$start/\$dsadad/g' your_file
>> ASD = $dsadad ( *.cpp )

sed 's/\*//g' your_file
>> ASD = $start ( .cpp )

To follow your edit :

sed -i 's/ASD = \$start ( \*.cpp )/ASD = \$dsadad ( .cpp )/' somefile
>> ASD = $dsadad ( .cpp )

Add the -i (--inplace) to edit the input file.

Escape twice special characters using sed

One backslash is used to change an argument to text or vice versa.

echo 'Uploaded registry/version1.3.1' | sed 's/\//\\\\\//g'
Uploaded registry\\/version1.3.1

In this case you need \\ for one backslash and \/for a slash.

Updating a yaml with a string containing special characters using sed

The default delimiter of sed is conflicting with the / in your variable. You will need to set a different delimiter, further, the single quotes will not allow the variable to expand.

You can try this sed

$ sed "\|Key1|s|$|'$var'|" input_file
Key1: 'fkugoiuhoiuyflkbbui/qy++bfv7J3c'
Key2:

Delete any special character using Sed

To do what you're trying to do in your answer (which adds [ and ] and more to the set of characters in your question) would be:

sed '/[][!?+,#$&*() ]/d'

or just:

grep -v '[][!?+,#$&*() ]'

Per POSIX to include ] in a bracket expression it must be the first character otherwise it indicates the end of the bracket expression.

Consider printing lines you want instead of deleting lines you do not want, though, e.g.:

grep '^[[:alnum:]_.-]$' file

to print lines that only contain letters, numbers, underscores, dashes, and/or periods.

How to add all special characters in variable for sed command

You could escape all /'s in your variable with a backslash \ (a literal \ must be escaped as \\):

sed -i "s/my-images=/&${varname//\//\\/}/" /home/myconfig

Sed replace strings starts with special characters

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.

Using sed to replace to a var with special characters

Since STRING contains a /, you should use a other delimiter, for example, you can use ^ like so:

sed  's^<<string>>^'"$STRING"'^g' file.txt

The quoting logic (''""'') is explained nicely on this SO answer.


Example on my locale machine:

$
$ cat file.txt
My string: <<string>>
$
$
$ STRING="something-else;device=name.of.device;key=abcd1234/wtp="
$
$
$ sed -i 's^<<string>>^'"$STRING"'^g' file.txt
$
$ cat file.txt
My string: something-else;device=name.of.device;key=abcd1234/wtp=
$
$

Replace special character / using sed command

As detailed in the comment Escaping forward slashes in sed command

You can use instead of

sed "s/target/replacement/" file 

either

sed "s|target|replacement|" file

or

sed "s@target@replacement@" file

Command:

$ echo "I / YOU." | sed 's@/@*@'
I * YOU.

More generally when looking at the sed accepted syntax:

sed

[2addr] s/BRE/replacement/flags

Substitute the replacement string for instances of the BRE in the
pattern space. Any character other than backslash or newline can be
used instead of a slash to delimit the BRE and the replacement. Within
the BRE and the replacement, the BRE delimiter itself can be used as a
literal character if it is preceded by a backslash.

You can also go for another approach in which you do not change the separators but you use the hexadecimal value of the character you want to replace, this will also avoid ambiguity. (http://www.asciitable.com/)

$ echo "I / YOU." | sed 's/\x2F/*/'
I * YOU.

sed replacing string with special characters

Following script should work for you:

key="12'{}34[];|^)(*&^!^#~\`!-567"
escappedKey=$(printf '%s\n' "$key" | sed 's/[]\/$*.^[]/\\&/g');

value="345$\`{}[]|%';"
escappedValue=$(printf '%s\n' "$value" | sed 's/[]\/$*.^[]/\\&/g');

sed "s/$escappedKey/$escappedValue/g" hello.txt

Note that you will need to escape tilde as \' in double quotes and also you are using $key to populate escappedValue by mistake.

Output:

ABC="345$`{}[]|%';"


Related Topics



Leave a reply



Submit