BASH: remove digits from end of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH: remove digits from end of string
# 1  
Old 03-28-2011
BASH: remove digits from end of string

Hi there, im sure this is really simple but i have some strings like this


Code:
e1000g123001

e1000g0

nge11101

nge3

and 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 second variable containing the digits at the end e.g. 3 or 123001 . The digits in the string will always be at the end and the alphas, always at the beginning .. i tried cutting particular parts of the string out based on number of characters, but as you can see the number varies

does anyone know how i can extract these values based on whether they are digits or alphas as opposed to for example , getting 3 characters in from the right etc (which isnt suitable)

any help would be greatly appreciated

---------- Post updated at 08:07 AM ---------- Previous update was at 07:24 AM ----------

UPDATE: actually ive just realised that the e1000g part of my example string does indeed have digits in, so its made my task even more complex as i need to now go up to the last alpha from the right ... as opposed to plain is it a number or is it a letter. Apolos, i didnt see that.

somebody has said to me i need to look at a 'non greedy regex match up to the last letter and create a back reference'

so im going to look at that as well
# 2  
Old 03-28-2011
Not sure about the expected output for you!

Code:
 
cat input_file
e1000g123001
e1000g0
nge11101
nge3
$head -1 input_file | sed 's/[0-9]//g'
eg
$head -1 input_file | sed 's/[a-z]//g'
1000123001

# 3  
Old 03-28-2011
thanks for replying panyam, the expected output would be 2 variables so for example with a string called

e1000g112001

I would have a

$DRIVER that has e1000g
$INSTANCE that has 112001

it becomes more difficult because as you can see, the driver name (in this case e1000g) has digits within it ... but one thing i know is that ALL drivers will end in a letter regardless of whether they contain a number.

So i think i need to do a pattern match of everything up to the last letter (which will be the $DRIVER variable) then everything after that last letter (which will be the $INSTANCE variable)
# 4  
Old 03-28-2011
I believe this should work:

storing the output got into variables is upto you.

Code:
 
 
cat input_file
e1000g112001
e1000g0
nge11101
nge3
 
$head -1 rem | sed 's/\(.*[a-z]\)\(.*\)/\1/'
e1000g

$head -1 rem | sed 's/\(.*[a-z]\)\(.*\)/\2/'
112001
 
$head -4 rem | sed 's/\(.*[a-z]\)\(.*\)/\1/'
e1000g
e1000g
nge
nge


$head -4 rem | sed 's/\(.*[a-z]\)\(.*\)/\2/'
112001
0
11101
3

# 5  
Old 03-28-2011
thank you so much panyam, is there any chance you could explain what this sed statement is doing? is this equivalent to a back reference replace ?

if you dont get a chance then not to worry, thanks anyway
# 6  
Old 03-28-2011
Hello,

Sorry I was at my dinner so was the delay.

here , using sed i am first searching for the last occurance of "alphabet" using \(.*[a-z]\) and the same can be referenced by \1, rest every thing can be referenced by \2.

A good place to start with , if you are interested to learn sed:

www.grymoire.com/Unix/Sed.html
# 7  
Old 03-28-2011
A portable sh alternative:
Code:
device=e1000g123001
INSTANCE=${device##*[[:alpha:]]}
mask=
while [ ${#mask} -ne ${#INSTANCE} ]; do
        mask=$mask?
done
DRIVER=${device%$mask}

Regards,
Alister

---------- Post updated at 01:06 PM ---------- Previous update was at 12:57 PM ----------

A bash alternative:
Code:
device=e1000g123001
INSTANCE=${device##*[[:alpha:]]}
DRIVER=${device:0:$((${#device}-${#INSTANCE}))}

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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

How can I extract digits at the end of a string in UNIX shell scripting or perl? cat file.txt abc_d123_4567.txt A246_B789.txt B123cc099.txt a123_B234-012.txt a13.txt What can I do here? Many thanks. cat file.txt | sed "s/.txt$//" | ........ 4567 789 099 012 13 (11 Replies)
Discussion started by: mingch
11 Replies

2. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

3. Shell Programming and Scripting

Remove 3rd character from the end of a random-length string

Hi, I hope someone can share there scripting fu on my problem, I would like to delete the 3rd character from a random length of string starting from the end Example Output Hope you can help me.. Thanks in advance.. (3 Replies)
Discussion started by: jao_madn
3 Replies

4. Shell Programming and Scripting

Remove lines that match string at end of column

I have this: 301205 0000030000041.49000000.00 2011111815505 908 301205 0000020000029.10000000.00 2011111815505 962 301205 0000010000027.56000000.00 2011111815505 3083 312291 ... (2 Replies)
Discussion started by: herot
2 Replies

5. Shell Programming and Scripting

How to remove white spaces from the beginning an end of a string in unix?

Suppose, I have a variable var=" name is ". I want to remove the blank spaces from the begining and endonly, not from the entire string. So, that the variable/string looks like following var="name is". Please look after the issue. (3 Replies)
Discussion started by: mady135
3 Replies

6. 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

7. Shell Programming and Scripting

Awk: Remove comma at the end of the string

Hi i had String like UID: ABC345QWE678GFK345SA90, LENGTH 32 when I used awk ' FS, {print $1}' prints ABC345QWE678GFK345SA90, how can i getrid of that coma at the end of the string. Thanks in advance.. (14 Replies)
Discussion started by: Reddy482
14 Replies

8. Shell Programming and Scripting

Remove box like special character from end of string

Hi All, How to remove a box like special character which appears at the end of a string/line/record. I have no clue what this box like special character is. It is transparent square like box. This appears in a .DAT file at the end of header. I'm to compare a value in header with a parameter.... (16 Replies)
Discussion started by: Qwerty123
16 Replies

9. Shell Programming and Scripting

Remove / at the end of a string if it exists

Hey guys, Can sed or another command be used to examine a path such as /home/test/blah/blahand check to see if the last character is a "/" and if so remove it So this is what should happen: /home/test/blah/blahnothing happens, this is ok /home/test/blah/blah/the "/" at the end is... (6 Replies)
Discussion started by: tret
6 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

Featured Tech Videos