Deleting the second to last character in each line – with sed

sed

How do I delete the character before the last character in each line in a file?

I tried sed 's/.$//' myfile1.txt which removed the last character of each line in myfile1.txt, but I am not sure how to delete the penultimate character in each line.

Best Answer

You can do:

sed -E 's/.(.)$/\1/' file.txt  

To edit the file in place, without backup:

sed -Ei 's/.(.)$/\1/' file.txt 

To edit the file in place, with original file backed up with .bak extension:

sed -Ei.bak 's/.(.)$/\1/' file.txt 

POSIX-ly:

sed 's/.\(.\)$/\1/' file.txt