Escape character - sed

Hi All,

How do i write in sed for the 6th and 7th field of etc/passwd file as it involves "/" character?

Does mine below is correct? It's incomplete script as i need help with syntax as i always getting may errors :frowning:

Example of etc/passwd file:

blah:x:1055:600:blah blah:/home/blah:/bin/ksh

sed "s/\($name:.:[0-9]*:\)[0-9]*:\(.*\):\(\/.*\/.*\):\(\/.*\/.*\)

The separator after s can be any character. Also, I'd recommend using single quotes around the script unless you specifically need double quotes (in which case the backslashes need to be doubled).

sed 's%\('"$name"':.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\) ...'

Notice the mix of single and double quotes above.

i tried that, but it says "SED: command garbled:

it looks as:

sed "s/\($name:.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\)/\1$answer\2/" /etc/passwd

I tried the single quote as era suggested, but SED is not happy

Repeat: you need to double the backslashes if you use double quotes. This is an artefact of the shell's quoting mechanisms, not of sed syntax as such. If you use the slash as separator, you do need to backslash any slashes which are not separators; but it's better to simply use a different separator character.

sed "s%\\($name:.:[0-9]*:\\)[0-9]*:\\(.*\\):\\(/.*/.*\\):\\(/.*/.*\\)%\1$answer\2%" /etc/passwd

With single quotes, that becomes

sed 's%\('"$name"':.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\)%\1'"$answer"'\2%' /etc/passwd

The variables $name or $answer obviously cannot contain the separator character in their values (or you need to escape the values).

Ok..

I use single quote..

sed 's/\('"$name"':.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\)' /etc/passwd

but still no good..

what did i do wrong???

You are not copy+pasting correctly, and not reporting back what the error message is. "Still no good" is not a useful diagnostic. But if you carefully use your mouse to copy+paste the commands above, and carefully copy+paste any errors back here, we might be able to help you.

"Unknown option to s" means you have something after (what sed thinks is) the final separator which is not a valid option for s/from/to/gp (here, "g" and "p" are options). In this case, it's simply because you still use slash as the separator, without escaping those slashes which are not separators. Change the separator to %, or escape the slashes as required.

Also, your command is partial; you are missing the "to" part \1$answer\2