Replacing / with TAB using sed

Viewed 53916

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.

10 Answers

To replace / with TAB using sed:

sed 's/\//\'$'\t/g' filename.txt

Generally, place \'$' before tab (\t) and newline(\n) characters. Works in bash.

To extract the directory name:

Personally, I'd use grok12's answer.

awk -F'/' '{print $5}' filename.txt

Another option is to use `printf "\t"` within sed:

sh -c 'echo test1 test2 | sed -E "s#(.*) (.*)#\2`printf "\t"`\1#g"'

The output is:

test2   test1

Here's to show it works for your own example:

sh -c 'echo /path/to/some/USEFUL_INFORMATION/in/some/path | sed "s#/#`printf "\t"`#g"'

Output:

path    to  some    USEFUL_INFORMATION  in  some    path
Related