Removing numbering from last character

Hi,
I have a file test.txt, below the contents:

SCDE1
SF9
STR1D2
SREDF21
FRED
STER2R4

I want to remove only the last character if it is the number, so the output should as below:

SCDE
SF
STR1D
SREDF2
FRED
STER2R

Any help or assistant will greatly appreciated.

Thanks

When starting a new thread in the Shell Programming and Scripting forum, please always tell us what operating system and shell you're using.

Also tell us what you have tried to solve this on your own.

Did you search for similar threads that may have already explained how to do what you're trying to do? There seem to be several threads in the list of topics you might find helpful at the end of output for this thread that exactly address what you want to do.

Hi,

This is for bash shell.
Yes, I tried below:
cat test.txt |awk '{print substr($1, 1, length-1)}'

test.txt:

SCDE1
SF9
STR1D2
SREDF21
FRED
STER2R4

Output:

SCDE
SF
STR1D
SREDF2
FRE
STER2R

This will allow me to remove whatever last character in the line, but the requirement is only remove if numeric (1-9).
Appreciate if any suggestion given.

Thanks

You still haven't told us what operating system you're using.

Note that numeric is 0 through 9; not 1 through 9.

Note that awk and sed are perfectly capable of reading a file by itself without creating a pipe and an unneeded process ( cat ) to slow down processing and waste system resources.

One might try any of the following (all of which are very similar to suggestions in the threads listed at the bottom of this page) each of which should do what you requested:

sed 's/[0-9]$//' test.txt
awk '{sub(/[0-9]$/, ""}1' test.txt
awk '/[0-9]$/{$0=substr($0, 1, length-1)}1' test.txt

If you're trying this on a Solaris/SunOS system, change awk in either of the above to /usr/xpg4/bin/awk or nawk .

1 Like

It worked for me, thank you so much! Will perform more searching in the forum moving forward.