tags:

views:

57

answers:

3

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

Also my file seems to have weird ^A delimiters in it. What char does ^A correspond to?

+2  A: 
sed 's/..$//' filename.txt
BenV
+2  A: 

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)

Lester Cheung
Note that you need to type Ctrl-A, not a literal ^ and a literal A.
Adam Rosenfield
A: 

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

ghostdog74