16

I have

Input:

 NISHA =\455  

Output:

 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
1
  • 3
    You seem to have a non-POSIX implementation of sed (or possibly you're running that command from a non-POSIX shell?). You don't need sed though, tr is enough: tr -d '\\' < P. Nov 21, 2014 at 12:15

5 Answers 5

28

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:

sed 's/\\//g'

Special characters

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.

  1. In a regex (input to commands like sed, 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.

  2. In a shell (like bash) you can escape backslash by backslash. So instead of \ write \\. 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 \ by \ 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:
sed 's/[\]//g'
sed 's/\\//g'

0
6

You have to escape the backslash. Try this:

sed 's/\\//g'
4

If you don't want sed solutions, then try with following commands:

$ echo "NISHA =\455"| awk -F'\' '{print $1 $2}'
$ echo "NISHA =\455"| tr '\\' ' '
$ echo "NISHA =\455"| tr -d '\\'
$ echo "NISHA =\455"| cut -c 1-7,9-11
2

In awk , i would do like this,

$ echo 'NISHA =\455' | awk '{gsub(/\\/," ")}1'
NISHA = 455
1
sed 's/\\//'

Place a \ before \ like you do with most special characters.

1
  • You have omitted the g flag for unknown reason. This changes the behaviour of the command but such a change was not asked for in the question. Otherwise your reply does not bring anything new in comparison to the earlier replies. Nov 21, 2014 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .