Test of more than one number on the end of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test of more than one number on the end of a string
# 1  
Old 03-23-2011
Test of more than one number on the end of a string

Hi there, I have a bunch of interface names like

e1000g0
nge1
dmfe3


I also have some that have longer (vlan tagged) names

like

e1000g123001
nge23003
e1000g999002


I need to determine whether the interface is one of the former or latter types and I would do that by seeing whether it has 1 number at the end or more than one number at the end.

Does anyone know of a simple one liner that will help me establish this?

any help would be greatly appreciated
# 2  
Old 03-23-2011
Post desired output.
# 3  
Old 03-24-2011
well I was more thinking of just a pattern match test ??

Code:
]#!/bin/perl -w

my $int = "nge123001";

if ( $int =~ /has more than one 0-9 at the end of the string$/ ) {
    print "PATTERN MATCHED - is a vlan tagged interface\n\n";
} else {
    print "NOT MATCHED - is a standard interface\n\n";
}

# 4  
Old 03-24-2011
Code:
$int =~ /\d\d$/

# 5  
Old 03-24-2011
thats brilliant thankyou (so simple) ...ive just been told I have to do this in bash as opposed to perl, Do you know if this is portable to bash?

I tried

Code:
#!/bin/bash


INT="nge1"

if [[ "${INT}" =~ \d\d\d$ ]]; then
  echo "tagged"
else
  echo "standard"
fi

but it didnt work

thanks again for your assistance

---------- Post updated at 08:58 AM ---------- Previous update was at 05:39 AM ----------

ok, ive figured it out in bash

Code:
#!/bin/bash


INT="nge4001"

if [[ "${INT}" =~ "[0-9][0-9][0-9]$" ]]; then
  echo "tagged"
else
  echo "standard"
fi



thanks for all your help

Last edited by rethink; 03-24-2011 at 10:01 AM..
# 6  
Old 03-24-2011
An awk solution

Code:
$ cat input|awk '/[0-9][0-9][0-9]$/ {print $0 " - VLan"}; /[^0-9][0-9]$/ {print $0 " - Standard"}'
e1000g0 - Standard
nge1 - Standard
dmfe3 - Standard
e1000g123001 - VLan
nge23003 - VLan
e1000g999002 - VLan

# 7  
Old 03-24-2011
Another option:
Code:
awk '{x=substr($0,length($0)-1,1)} x~/[0-9]/{print $0,"Vlan"} x~/[^0-9]/{print $0,"Standard"}'
e1000g0 Standard
nge1 Standard
dmfe3 Standard
e1000g123001 Vlan
nge23003 Vlan
e1000g999002 Vlan

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding test to beginning and end of list in a file

Hi all and apologies for the silly question, but I've searched and I can't get this right. I have a list of email addresses in a file that I need to blacklist (spam). the list is quite long and I would need to script a small routine so that I can get the following for each line in the file: db... (4 Replies)
Discussion started by: bm555
4 Replies

2. UNIX for Dummies Questions & Answers

Bash condition test at the end of string

I want to check (using bash condition test function) if string contains three spaces, ignoring last three spaces at the end of string. string_to_report='foo bar foo bar ' string_to_ignore='foo bar ' (8 Replies)
Discussion started by: useretail
8 Replies

3. Solaris

Test program running taking much more time on high end server T5440 than low end server T5220

Hi all, I have written the following program and run on both T5440 and T5220 on same OS version. I found that T5540 server takes more time than T5220. Please find below the details. test1.cpp #include <iostream> #include <pthread.h> using namespace std; #define NUM_OF_THREADS 20... (17 Replies)
Discussion started by: sanjay_singh85
17 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

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

6. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

7. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

8. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

9. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

10. Shell Programming and Scripting

Test decimal number

Hi, I would like test if a number is a decimal number or not (9 Replies)
Discussion started by: francis_tom
9 Replies
Login or Register to Ask a Question