如何在特定字符后跟数字后删除字符串中的所有内容?

时间:2017-05-02 18:51:18

标签: unix sed

我有这个文件:

Cavr.NG178.1       Cavr.NG12780.1_at      APTG386_at-255
Cavr.NG056.1       Cavr.NG02560.1_at      APTG560_at-895
Cavr.NG714.1       Cavr.NG77140.1_at      APTG680_s_at-2732

我想在最后一栏的“at-”之后删除所有内容,例如:

Cavr.NG178.1       Cavr.NG12780.1_at      APTG386_at
Cavr.NG056.1       Cavr.NG02560.1_at      APTG560_at
Cavr.NG714.1       Cavr.NG77140.1_at      APTG680_s_at

但我尝试的代码也影响了第二列,例如:

sed 's/at-*//'

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

sed 方法:

sed 's/_at[^[:space:]]*$/_at/' file

输出:

Cavr.NG178.1       Cavr.NG12780.1_at      APTG386_at
Cavr.NG056.1       Cavr.NG02560.1_at      APTG560_at
Cavr.NG714.1       Cavr.NG77140.1_at      APTG680_s_at

答案 1 :(得分:1)

使用GNU sed:

sed -E 's/(.*at).*/\1/' file

输出:

Cavr.NG178.1       Cavr.NG12780.1_at      APTG386_at
Cavr.NG056.1       Cavr.NG02560.1_at      APTG560_at
Cavr.NG714.1       Cavr.NG77140.1_at      APTG680_s_at

答案 2 :(得分:0)

$ sed 's/-[^-]*$//' file
Cavr.NG178.1       Cavr.NG12780.1_at      APTG386_at
Cavr.NG056.1       Cavr.NG02560.1_at      APTG560_at
Cavr.NG714.1       Cavr.NG77140.1_at      APTG680_s_at

或:

$ cut -d- -f1 file
Cavr.NG178.1       Cavr.NG12780.1_at      APTG386_at
Cavr.NG056.1       Cavr.NG02560.1_at      APTG560_at
Cavr.NG714.1       Cavr.NG77140.1_at      APTG680_s_at