Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a tab character with sed on OS X?

Tags:

regex

macos

sed

I have tried:

echo -e "egg\t  \t\t salad" | sed -E 's/[[:blank:]]+/\t/g' 

Which results in:

eggtsalad 

And...

echo -e "egg\t  \t\t salad" | sed -E 's/[[:blank:]]+/\\t/g' 

Which results in:

egg\tsalad 

What I would like:

egg salad 
like image 932
Zach Avatar asked Mar 22 '11 22:03

Zach


People also ask

How do you add a tab character on a Mac?

Press and hold your option key, then type 0009 (zeroes). This is the Unicode sequence for the tab control character.

Does sed command work on Mac?

One way to make the GNU version of the SED to work on the Mac OS X, is to directly install the gnu-sed along with the default names which will assure you that you won't have to run different commands on both the operating systems.

How do I add a tab in Linux?

Use Ctrl+V (or Control+V on a Mac) and then the tab key. That will put a literal tab on the command line, which can be useful if (like me) you need to grep for a single character in a field in a tab delimited text file.


2 Answers

Try: Ctrl+V and then press Tab.

like image 63
yan Avatar answered Oct 06 '22 06:10

yan


Use ANSI-C style quoting: $'string'

sed $'s/foo/\t/' 

So in your example, simply add a $:

echo -e "egg\t  \t\t salad" | sed -E $'s/[[:blank:]]+/\t/g' 
like image 34
wisbucky Avatar answered Oct 06 '22 06:10

wisbucky