Replacing / with TAB using sed
I need to replace a slash character with a tab to开发者_如何学C 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.
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
Try awk
:
awk '{gsub("/","\t",$0); print;}' filename
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
I found the answer, for me, here. I have MacOSX so I used the Control+v followed by Control+i.
sed -e 's/\/[CTR+V][CTR+I]/g' filename
awk -F '/' '{print $5}' your_file
This prints USEFUL_INFORMATION
It defines the field separator to be /. Note that since your strings start with a / the first field is null so your field counts will be one more than you might think.
Try to put double quote(" ") around instead of single quote(' ') as shown in the following command.
sed -i "s/\//\\t/g" filename
Generally we use double quotes(" ") with the sed when we are dealing with the special characters.
As in your case you haven't escaped the \ in TAB character(\t).
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
Use + as delimiters
sed -e 's+/+\t+g
'
echo 'path/to/something' | sed -e 's+/+\t+g'
// path to something
Edit: a simpler way to extract the path of a file:
dirname /path/to/file.txt
#output /path/to
Have a look at dirname
and basename
$ dirname /path/to/some/USEFUL_INFORMATION/in/some/path
/path/to/some/USEFUL_INFORMATION/in/some
$ basename /path/to/some/USEFUL_INFORMATION/in/some/path
path
awk can handle any separator, e.g. /
:
$ echo /path/to/some/USEFUL_INFORMATION/in/some/path | awk -F"/" '{print $5}'
USEFUL_INFORMATION
First as the others suggested, use another character as a delimiter. Second, use $'\t'
outside of the quotes. Example with ;
as a delimiter:
$ echo "/path/to/some/USEFUL_INFORMATION/in/some/path" | sed -e 's;/;'$'\t'';g'
path to some USEFUL_INFORMATION in some path
精彩评论