How to sed remove last character from each line

Originally published at: https://www.cyberciti.biz/faq/sed-remove-last-character-from-each-line/

I need to remove the last character from a line and shell variable such as “${_allowed_ips}”. For example, each line is as follows in my file:
foo,
bar,
demo,

I need output as follows:
foo
bar
demo
.
How can I use sed to delete the last character?

if it s aways ,
sed -i s/,$//g file

if you want to remove the last character
sed -i s/.$//g file


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki