2010-04-08 111 views

回答

15
sed 's/..$//' filename.txt 
+0

如果有什麼人想刪除最後一個字符1232? – qed 2013-09-19 10:06:16

+4

瞭解它:'sed's /。\ {1232 \} $ //'filename' – qed 2013-09-19 10:07:59

2

第二個BenV的答案。然而,你可以確保你只能通過刪除^ A:

sed 's/^A^A$//' <file> 

除此之外,找出^ A是什麼,我做了以下內容:

% echo -n '^A' |od -x 
0000000 0001 
0000001 

% ascii 0x01 
ASCII 0/1 is decimal 001, hex 01, octal 001, bits 00000001: called ^A, SOH 
Official name: Start Of Heading 

(想添加爲註釋,但它不會做正確引用)

+1

請注意,您需要鍵入Ctrl-A,而不是文字^和文字A. – 2010-04-08 04:01:32

0

你也可以用awk

awk '{sub(/..$/,"")}1' file 

,您還可以使用shell

while read -r line; do echo ${line:0:(${#line}-2)}; done<file 

但是如果你正在談論擺脫DOS新行(即\ r \ n)的,你可以使用dos2unix命令

相關問題