【问题标题】:How to remove the first column from all files and replace?如何从所有文件中删除第一列并替换?
【发布时间】:2014-09-06 20:14:02
【问题描述】:

我有几个制表符分隔的文件,例如文件如下所示:

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'

【问题讨论】:

    标签: unix sed cut csv


    【解决方案1】:

    这样就可以了:

    $ 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
    

    在所有情况下,请注意您必须指出什么是字段分隔符。

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 1970-01-01
      • 2021-07-25
      • 2015-12-02
      • 2015-12-21
      • 2018-05-03
      • 2021-01-10
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多