我有几个制表符分隔的文件,例如该文件如下所示:
A213<TAB>foo bar<TAB>sentence
A123<TAB>bar foo<TAB>sentence
B84521<TAB>abc hello<TAB>world
C984<TAB>def word<TAB>hello
我需要它删除第一列并用|||
替换标签,输出应该如下:
foo bar ||| sentence
bar foo ||| sentence
abc hello ||| world
def word ||| hello
我已经尝试了以下但是它没有工作:
$ cut -f2,3 file.txt | sed 's/<TAB>/\s|||\s/g'
答案 0 :(得分:3)
这可以做到:
$ awk 'BEGIN{FS="\t"; OFS=" ||| "} {print $2, $3}' file
foo bar ||| sentence
bar foo ||| sentence
abc hello ||| world
def word ||| hello
这只是相应地定义输入和输出字段分隔符的问题。然后,打印第二和第三个字段。
使用cut
+ sed
,您可以使用:
$ cut -d$'\t' -f2- < file | sed 's/\t/|||/'
foo bar|||sentence
bar foo|||sentence
abc hello|||world
def word|||hello
在所有情况下,请注意您必须指明字段分隔符是什么。