# sed interpreting slash commands



## chatwizrd (Aug 8, 2013)

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:


```
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.


----------



## fonz (Aug 8, 2013)

Please post what you _want_ the output to be.


----------



## wblock@ (Aug 8, 2013)

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'`

```
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`


----------



## yom (Aug 9, 2013)

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'`


```
root	*	0	0	Charlie &	/root	/bin/csh
```


----------



## SirDice (Aug 9, 2013)

Why not simply do this?

[cmd=]cat /etc/passwd | column -s':' -t[/cmd]


----------



## ta0kira (Aug 17, 2013)

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


----------

