sed interpreting slash commands

How do you get sed to interpret slash commands like \n \t for example? I did a command like the following: cat /etc/passwd | grep ^root | sed -E -e 's/:/&\t/g'. The output will just be the following with a bunch of t characters:

Code:
root:t*:t0:t0:tCharlie &:t/root:t/usr/local/bin/bash

It works fine on Linux but not FreeBSD and I have no clue what I am doing wrong.
 
You're not doing anything wrong. sed(1) on FreeBSD is unable to do the most basic useful things. Why it has never been extended to be useful, I don't know. There is textproc/gsed, but I suggest just using Perl, which can do the same command-line things as sed(1), but is vastly more powerful.

cat /etc/passwd | grep ^root | perl -pe 's/:/\t/g'
Code:
root	*	0	0	Charlie &	/root	/bin/csh

Or, to avoid the UUOC:
grep ^root /etc/passwd | perl -pe 's/:/\t/g'

To avoid grep(1):
perl -ne 'if (/^root/) { s/:/\t/g; print }' /etc/passwd
 
There is still a possibility with the sed command and tabulations which consist to introduce a TAB char in your terminal with {CTRL+V TAB}. But it depends on how your terminal handles this I guess.

grep "^root" /etc/passwd | sed -e 's:\:: :g'

Code:
root	*	0	0	Charlie &	/root	/bin/csh
 
Why not simply do this?

[cmd=]cat /etc/passwd | column -s':' -t[/cmd]
 
I have this problem frequently. If you're using sh or bash, you can have the shell insert the special character with e.g. $'\n' or $'\t'. Unfortunately this is a little awkward if your expression is '-quoted, so you'd need something like sed -E -e 's/:/&'$'\t''/g'. Alternatively, you can use gsed.

Kevin Barry
 
Back
Top