2017-05-02 45 views
0

我有這個文件:如何在特定字符後跟數字後刪除字符串中的所有內容?

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-*//' 

有什麼建議嗎?

回答

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

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 
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 
相關問題