How to remove trailing characters in one line

I have file with patterns as shown:

a0
b00
c000
d0000000
e00
f00

How to write a perl oneliner or sed command to obtain output as:

a
b
c
d
e
f

My trial:

sed -i 's/0*$//g' filename.txt

Answers


very likely there are trailing spaces in your lines, give this sed oneliner a try:

sed 's/0*\s*$//' file

Note that the g flag is not necessary.


One way using perl:

perl -F"" -alne 'print $F[0]' file

This is the perl equivalent (inplace replacement):

perl -pi -e 's/^(.).*$/$1/' file

And if you are curious an awk:

awk 'BEGIN{FS=""}{print $1}' file

Try this:

sed -r 's/(.).*/\1/' File

Only print the first character from each line.


Need Your Help

Tkinter update values from other class

python tkinter config python-multithreading

I've got a tkinter window that have 3 features: background color,foreground color, and a text label. These features is in a text config file (properties.conf) in my home folder. I want to update wi...

Add a symbolic breakpoint on a selector in Xcode

xcode lldb

There's a bug in my app which shows up with the following (partial) stacktrace: