How can I extract digits at the end of a string in UNIX shell scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I extract digits at the end of a string in UNIX shell scripting?
# 8  
Old 11-23-2018
Quote:
Originally Posted by mingch
I tried to create the following scripts but it can't increment the last number from 012 to 013, anyone help? Many thanks.
The reason is that "012" is not a number. "12" is a number and you can increment that to "13". But "012" and "013" are strings and you cannot do calculations with strings.

You haven't told us yet what your shell is and in fact it didn't matter that much because what we did up to now was common ground to all of them. We are leaving this area and maybe i will tell you something you can't use because you have a different shell - which is why you should always state what your environment (OS and shell and their versions most prominently) is.

In Korn shell (ksh) you can use typeset to make a variable have leading zeroes and having a certain length like this (try it on the command line):

Code:
$ typeset -RZ3 variable=12
$ echo $variable
012
$ (( variable += 1 ))
$ echo $variable
013

Variables in shell do not really have a certain "type". Of course it will lead to an error if you try to multiply "abc" by 3 but if you create a string "ab12cd", then somehow (you now know how) get rid of the characters you can multiply the remaining "12" and it will give you "60", which you still can use as a string. What we did above was to create a string, right-aligned (-R) in the length of 3 where every "free"space is filled with zeroes. When we calculate with it like a number, the shell will silently drop the leading zeroes to make the number "12" from "012", then do the calculation and, when writing the result back, realign and refill it (because of the typeset directive, so that "013" is the final content.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 9  
Old 11-23-2018
Code:
	BNFILE=$(echo "${FNFILE%.txt}" | sed 's/[1-9][0-9]\{,10\}$//')
	OLDNUM=$(echo "${FNFILE%.txt}" | sed 's/.*[^1-9]\([0-9][0-9]*\)/\1/')


Last edited by nezabudka; 11-29-2018 at 03:12 PM..
This User Gave Thanks to nezabudka For This Post:
# 10  
Old 11-30-2018
Hi nezabudka,
Thanks for your help, but I found 1.txt does not change to 2.txt, please help again. Many thanks.
OLD file is /tmp/path13/1.txt, NEW file is /tmp/path13/12.txt


Code:
BNFILE=$(echo "${FNFILE%.txt}" | sed 's/[1-9][0-9]\{1,10\}$//')
	OLDNUM=$(echo "${FNFILE%.txt}" | sed 's/.*[^1-9]\([0-9][0-9]*\)/\1/')


Result
Code:
OLD file is /tmp/path1/abc_d123_4567.txt, NEW file is /tmp/path1/abc_d123_4568.txt
OLD file is /tmp/path2/A246_B789.txt, NEW file is /tmp/path2/A246_B790.txt
OLD file is /tmp/path12/B123cc099.txt, NEW file is /tmp/path12/B123cc0100.txt
OLD file is /tmp/path12/a123_B234-012.txt, NEW file is /tmp/path12/a123_B234-013.txt
OLD file is /tmp/path13/a13.txt, NEW file is /tmp/path13/a14.txt
OLD file is /tmp/path13/1.txt, NEW file is /tmp/path13/12.txt

# 11  
Old 12-03-2018
Code:
BNFILE=$(echo "${FNFILE%.txt}" | sed 's/[1-9][0-9]\{1,10\}$//')

Code:
{,10\}

# 12  
Old 12-03-2018
"Enhanced" version of previous proposal:



Code:
while read FN
  do    TMP=${FN%${FN#${FN%.*}}}
        NMB=${TMP#${TMP%[^0-9]*}}
        NMB=${NMB#?}
        LEN=${#NMB}
        PRFX=${TMP%$NMB}
        printf "%s --> %s%0*d%s\n"  ${FN} ${PRFX} $LEN $((10#$NMB + 1)) ${FN#${FN%.*}}
  done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract n-digits from string in perl

Hello, I have a log file with logs such as 01/05/2017 10:23:41 : file.log.38: database error, MODE=SINGLE, LEVEL=critical, STATE: 01170255 (mode main how can i use perl to extract the 8-digit number below from the string 01170255 Thanks (7 Replies)
Discussion started by: james2009
7 Replies

2. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

3. Shell Programming and Scripting

How to extract text from STRING to end of line?

Hi I have a very large data file with several hundred columns and millions of lines. The important data is in the last set of columns with variable numbers of tab delimited fields in front of it on each line. Im currently trying sed to get the data out - I want anything beetween :RES and... (4 Replies)
Discussion started by: Manchesterpaul
4 Replies

4. Shell Programming and Scripting

How to add trailer record at the end of the flat file in the unix ksh shell scripting?

Hi, How to add trailer record at the end of the flat file in the unix ksh shell scripting can you please let me know the procedure Regards Srikanth (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

5. Shell Programming and Scripting

BASH: remove digits from end of string

Hi there, im sure this is really simple but i have some strings like this e1000g123001 e1000g0 nge11101 nge3and i want to create two variables ($DRIVER and $INSTANCE). the first one containing the alpha characters that make up the first part of the string, e.g. e1000g or nge and the... (9 Replies)
Discussion started by: rethink
9 Replies

6. Shell Programming and Scripting

help with Scripting - trying to search for string and extract next few characters

Hi I am new to world on unix scripting so any assistance would be gratefully appreciated, I am trying to write a script which reads through a file, reads in line by line, searches for a pattern, copies string after it and then to do a search and replace elsehwere in the line, so the... (7 Replies)
Discussion started by: LonJ_80
7 Replies

7. Shell Programming and Scripting

extract digits from a string in unix

Hi all, i have such string stored in a variable var1 = 00000120 i want the o/p var1 = 120 is it possible to have such o/p in ksh/bash ... thanx in advance for the help sonu (3 Replies)
Discussion started by: sonu_pal
3 Replies

8. Shell Programming and Scripting

Search and remove digits (if exist) from end of the string

Hi Experts, Here is what I am trying to do. 1) say I have a file with below strings database1 database2 database3 data10gdb1 data10gdb2 databasewithoutdigit 2) I want to get the below output. (- if there is any digit at the end of the string, I need to remove it) (- Any... (3 Replies)
Discussion started by: shail_boy
3 Replies

9. Shell Programming and Scripting

Need UNIX shell scripting end to end information

Hi, I would like to learn shell scripting in UNIX. Can any one please give me the support and share the information/documents with me. If any documents please post it to aswanikumar_nimmagadda@yahoo.co.in Thanks in advance...!!! (3 Replies)
Discussion started by: aswani_n
3 Replies

10. Shell Programming and Scripting

Extract digits at end of string

I have a string like xxxxxx44. What's the best way to extract the digits (one or more) in a ksh script? Thanks (6 Replies)
Discussion started by: offirc
6 Replies
Login or Register to Ask a Question