开发者

Removing last two chars from each line in a text file

开发者 https://www.devze.com 2022-12-25 20:22 出处:网络
How do I remove the last two chars from each line in a text file using just Linux commands? Also my file see开发者_如何学JAVAms to have weird ^A delimiters in it. What char does ^A correspond to?sed

How do I remove the last two chars from each line in a text file using just Linux commands?

Also my file see开发者_如何学JAVAms to have weird ^A delimiters in it. What char does ^A correspond to?


sed 's/..$//' filename.txt


Second BenV's answer. However you can make sure that you only remove ^A by:

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

In addition to that, to find out what ^A is, I did the following:

% 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

(wanted to add as a comment but it doesn't do quoting properly)


you can also use awk

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

you can also use the shell

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

however if you are talking about getting rid of DOS newlines (ie \r\n), you can use dos2unix command

0

精彩评论

暂无评论...
验证码 换一张
取 消