Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing / with TAB using sed

Tags:

sed

awk

I need to replace a slash character with a tab to I can extract a directory name using awk.

/path/to/some/USEFUL_INFORMATION/in/some/path

I tried doing this, but all it does is put a "t" at the from of my output.

sed -e 's/\//\t/g' filename

Any help would be appreciated.

like image 781
Lance Übercut Avatar asked Sep 09 '25 16:09

Lance Übercut


1 Answers

First, you can use another character as the s/// delimiter. The command

sed 's:ABC:DEF:g'

is equivalent for

sed 's/ABC/DEF/g'

It will make your command more readable because you'll not have to escape the slash.

Said that, some seds do not support character escaping such as \t. It happens to me a lot in Mac OS X, for example. My solution is to press Control+V and then the tab char:

sed 's:/:<Control+V><TAB character>:g'

The result in my machine is:

$ pwd
/Users/brandizzi/sandbox/demo/mydemo
$ pwd | sed 's:/:        :g'
    Users   brandizzi   sandbox demo    mydemo

However, if your intention is to put all path directories in awk variables $1, $2 etc. just declare the slash to be the field separator with awk -F flag:

$ pwd
/Users/brandizzi/sandbox/demo/mydemo
$ pwd | awk -F / '{print $3 " " $5}'
brandizzi demo
like image 192
brandizzi Avatar answered Sep 13 '25 17:09

brandizzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!