Remove trailing number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove trailing number
# 1  
Old 03-13-2014
Remove trailing number

I have some strings such as

ABC1
ABC2

TYFASDD12

They will only have letters and numbers. In each case I want to remove the last digit? The lengths will vary. So a hard coded substr won't work. What do I do?

if it doesn't end in a number, I don't want to remove any characters.

Last edited by guessingo; 03-13-2014 at 05:19 PM..
# 2  
Old 03-13-2014
To remove last digit:
Code:
sed 's#[0-9]$##' file

To remove all trailing numbers:
Code:
sed 's#[0-9]*$##' file

# 3  
Old 03-13-2014
Code:
str=TYFASDD12

# One trailing digit
echo "${str%[0-9]}"

# All trailing digits:
echo "${str%"${str##*[!0-9]}"}"

# 4  
Old 03-13-2014
Boy is the shell SOOO flexible:-
OSX 10.7.5, default bash terminal, longhand...
(A little more complex than Scrutinzer's but another approach.)
Code:
Last login: Thu Mar 13 20:49:11 on ttys000
AMIGA:barrywalker~> strng="ABC1"
AMIGA:barrywalker~> echo "$strng"
ABC1
AMIGA:barrywalker~> strng="${strng:0:$(( ${#strng} - 1 ))}"
AMIGA:barrywalker~> echo "$strng"
ABC
AMIGA:barrywalker~> _

# 5  
Old 03-14-2014
Quote:
Originally Posted by Scrutinizer
Code:
str=TYFASDD12

# One trailing digit
echo "${str%[0-9]}"

# All trailing digits:
echo "${str%"${str##*[!0-9]}"}"

IMHO you can remove all trailing digits simply by using 2 percent signs instead of one...
Code:
$ str=ABCDEFGHI0123456789
$ echo ${str%%[0-9]*}
ABCDEFGHI

# 6  
Old 03-14-2014
Hi shamrock.. no, that would not work:
Code:
$ str=ABC9DEFGHI0123456789
$ echo ${str%%[0-9]*}
ABC


Last edited by Scrutinizer; 03-14-2014 at 06:09 PM..
# 7  
Old 03-15-2014
Quote:
Originally Posted by Scrutinizer
Hi shamrock.. no, that would not work:
Code:
$ str=ABC9DEFGHI0123456789
$ echo ${str%%[0-9]*}
ABC

Your string is different from the one i has but I see your point...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove trailing space in Gawk

Hi, I have simply made a shell script to convert *.csv to *.xml file. Xml file is required for input to one tool. But i am getting space after last field. How can i remove it. Shell script is as follows :- if then echo "" echo "Wrong syntax, Databse_update.sh... (6 Replies)
Discussion started by: stillrules
6 Replies

2. Shell Programming and Scripting

Remove trailing space

Hi I am trying to remove trailing space from a string. value=${value%% } It is not working. What might be the issue with the above snippet. (7 Replies)
Discussion started by: munna_dude
7 Replies

3. Shell Programming and Scripting

Remove trailing 0 from the field

Hi Freinds, I have file1.txt as below file1.txt 1521894~~-0.400~201207 1521794~~-0.486~201207 152494~~-0.490~201207 152154894~~-0.490~201207 1521894354~~-0.489~201207 expected output : 1521894~~-0.4~201207 1521794~~-0.486~201207 152494~~-0.49~201207... (9 Replies)
Discussion started by: i150371485
9 Replies

4. Shell Programming and Scripting

Remove trailing zeros

Hi I have a simple request but can't find the answer. I want to remove trailing zeros, and in some cases the fullstops, from the input data. Example of input file: FR002_15.000_20.000 SD475_5.000_10.500 FG5647_12.250_15.500 BH2463_30.555_32.000 Desired output file would be: ... (10 Replies)
Discussion started by: theflamingmoe
10 Replies

5. Shell Programming and Scripting

Add trailing zero in a number

how to add a trailing zero in a number? For example: Input- 00000734560 output- 000007345600 Thanks in advance. (1 Reply)
Discussion started by: reeta_shri
1 Replies

6. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

7. Shell Programming and Scripting

sed - how to remove trailing slashes

I know you can remove trialing slashes using: #echo "/tmp/one/two/three////" | sed "s,/$,," /tmp/one/two/three/// But I want to know how to make it remove all trialing flashes in the front, and in the start, so the end result is: tmp/one/two/three Anyone have any idea how to do this... (6 Replies)
Discussion started by: EXT3FSCK
6 Replies

8. Shell Programming and Scripting

Remove trailing G

Hello, I am trying to write a script that will calculate the amount of data remaining in a storage volume. I'm running Tru64 Unix version 5.1B patch kit 6. The script is being run against an AdvFS domain. I am programming in Korn Shell version M-11/16/88f. The basic idea is that I want to run df... (3 Replies)
Discussion started by: Heathe_Kyle
3 Replies

9. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

10. Shell Programming and Scripting

remove trailing newline characters

Hello , I have the folowing scenario : I have a text file as follows : (say name.txt) ABC DEF XYZ And I have one more xml file as follows : (say somexml.xml) <Name>ABC</Name> <Age>12</Age> <Class>D</Class> <Name>XYZ</Name> <Age>12</Age> <Class>D</Class> <Name>DEF</Name>... (7 Replies)
Discussion started by: shweta_d
7 Replies
Login or Register to Ask a Question