【问题标题】:How to use tr command on a string in linux?如何在linux中对字符串使用tr命令?
【发布时间】:2018-05-24 01:18:53
【问题描述】:

我正在尝试对文本文件中的字符串使用tr 命令,以便可以将其替换为换行符。例如,我的文件中有这样一行

biological_process      GO:0000001      mitochondrion inheritance       is_a    GO:0048308      organelle inheritance

现在我想把它转换成这样的

biological_process      GO:0000001      mitochondrion inheritance
GO:0048308      organelle inheritance

我之前使用过tr 命令,我认为它会起作用,但现在当我尝试像这样运行它时它会起作用

test="biological_process      GO:0000001      mitochondrion inheritance       is_a    GO:0048308      organelle inheritance"
echo $test | tr 'is_a' '\n'
b
olog
c
l
proce

 GO:0000001 m
tochondr
on 
nher
t
nce 

 GO:0048308 org
nelle 
nher
t
nce

【问题讨论】:

标签: linux tr


【解决方案1】:

tr 翻译字符,而不是字符串。

$ sed 's/is_a/\n/g' <<< "biological_process      GO:0000001      mitochondrion inheritance       is_a    GO:0048308      organelle inheritance"
biological_process      GO:0000001      mitochondrion inheritance       
    GO:0048308      organelle inheritance
$ sed 's/is_a\s\+/\n/g' <<< "biological_process      GO:0000001      mitochondrion inheritance       is_a    GO:0048308      organelle inheritance"
biological_process      GO:0000001      mitochondrion inheritance       
GO:0048308      organelle inheritance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 2014-10-03
    相关资源
    最近更新 更多