更换/使用SED TAB [英] Replacing / with TAB using 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
我试图这样做,但它的作用是从我的输出把T的。
I tried doing this, but all it does is put a "t" at the from of my output.
sed -e 's/\//\t/g' filename
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
首先,你可以使用其它字符作为取值///
分隔符。命令
First, you can use another character as the s///
delimiter. The command
sed 's:ABC:DEF:g'
是相当于
sed 's/ABC/DEF/g'
它会使你的命令更具可读性,因为你没有逃跑的斜线。
It will make your command more readable because you'll not have to escape the slash.
说,有些SEDS不支持字符转义,如 \\ t
。它发生在我许多在Mac OS X,例如。我的解决办法是preSS控制+ V,然后标签字符:
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
不过,如果你的目的是要放在awk中变量的所有路径的目录 $ 1
, $ 2
等只是声明斜线是字段分隔符使用awk -F
标记:
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
这篇关于更换/使用SED TAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!