Removing last character from each line of file

How can I remove the last character from each line of a file?

This must be done without "funny" characters, as I want to transfer the code to/from Windows.

Any ideas?

sed -e 's/\(.*\).$/\1/g' file

As it's been outlined in the thread, transfer/ftp your files in ASCII mode.

Trouble is, I'm transferring a zip file containing binary and ascii files - I FTP the zip file as binary, then use unzip - so I'm in the lap of the gods...

Thanks

do you have TWO files to transfer or ONE zip?

if you have 2, transfer zip in binary and others in ascii.

AM I misisng the point here?

I've got ONE zip file containing both binary and ASCII files. I transfer it as binary, then unzip it. If I try to unzip using the -a switch, one of the binary files gets interpreted as ASCII which is a bigger problem.

Any clearer? I really appreciate your help

and if you use '-b' or '-B'?
I'm not a real guru when it comes to 'unzip' - just reading 'man unzip'.

Me neither. I suspect that's what it's doing now - transferring everything as binary.

Anyway, your sed script seems to do the trick, so I'm happy.

Thanks again for your help - it's cool when someone half way round the world solves a problem for me!

I want to strip off the last character, but only if it's a "^M".

Any ideas?

look at the command dos2unix

to strip ALL ^M-s:
tr -d '\015' < file

to strip all trailing ^M-s from all the files in the current directory:

#!/bin/ksh

find . -type f | while read file
do
   echo "stripping ^M from [${file}]..."
   ex - "${file}" <<EOF
%s/^M$//g
wq!
EOF
done;

^M - in 'vi' is CTRL-V followed by CTRL-M

dos2unix won't work. I want to transfer a zip file containing both ascii and binary files to unix. I do this as binary, then unzip the zip file. I then run a script on these files - as part of that script I can strip off the last (^M) characters in files I know will have them. But I might want to run the script again - and don't want to strip off the second-from-last character.

Thanks vgersh99 - the little tr statement does exactly what I want.

Cheerio from little ol' England.

Hi,

Can u rephrase the sed-command above such that it will display only the last character of the line? For example: if the line is:

hello world!

It will only display:

!

Appreciate your help.

Thanks.

echo '123' | sed 's/.*\(.\)$/\1/g'

It works! Thanks! :slight_smile:

Hi,

I can't believe it's been 2005 since I've last replied to this thread (the only thread I've subscribed til now, hehe).

Anyway, how do I strip off the last TWO characters of each line? I tried: echo 123|sed -e 's/\(.*\).$/\2/g' but it doesn't work.

Hope you guys can help.

Thanks! :o

Works perfectly!

Thanks!!!!:b:

What if I want to remove the last TWO characters of each line?