NISHA = 455
I want to remove
\ from output. I tried to use command
sed "s/[\]//g" P but it is not working and it flags an error:
character found after backslash is not meaningful
You can either replace the backslash by a space as you show in the example result:
sed 's/\\/ /g'
or you can remove it as you show in your code:
There are possible problems with escaping of backslash to cancel its special meaning. Backslash is a special character used for escaping both in a shell and in regular expressions.
The command you type to the shell's command line or a script is first processed by the shell which interprets the special meaning of characters and their escaping. The result is then passed to the command to be executed (like
sed) which performs its own interpretation of the characters. When you are constructing a command the mental procedure is the opposite way: first add the escaping for the regex then add the escaping for the shell.
In a regex (input to commands like
grep etc.) backslash can be escaped by backslash like this:
\\ and also you can use the set expression
[\] like you used because there backslash loses its special meaning.
In a shell (like
bash) you can escape backslash by backslash. So instead of
\\. Enclosing the string between double quotes
" makes backslash behaviour more complicated<1> but double backslash will still produce a single backslash. Enclosing the string between single quotes
' makes every character to be treated literally except
If you want to use double quotes, you can use one of the following:
sed "s/\\\\//g" - Escape
\ in the shell, and escape every
\ in the regex again. In fact the double quotes are not necessary in this case because every special character is properly escaped.
sed "s/[\\]//g" - Escape in the shell by a backslash
\ and in the regex use a set
sed "s/[\]//g" - Yes, your example should work in a POSIX compliant environment! Between double quotes
\ represents itself unless it precedes a special character in the context of double quotes:
$`"\ or a newline. It looks like that in your case either the shell or the
sed does not follow the POSIX standard.
With single quotes you can also use the string as you used or a shorter way: