Grab a number at end of filepath


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab a number at end of filepath
# 1  
Old 10-21-2008
Grab a number at end of filepath

Hello, I have a file path such as: /path/to/whatever/30 and I want to get the number off the end. My problem is that there might be other numbers in the path and the last number can be 1 or 2 digits. I tried something like:
Code:
sed 's/.*\(\/[0-9]\{1,2\}\).*/\1/'

which seems to work fine for single digit numbers, but if it is 10, I only get a 0. Also that might fail if there is a number in the path. Is there something simpler than using sed for this situation? Any ideas?

Thanks!
# 2  
Old 10-21-2008
What does
Code:
basename $filepath

return? It should be the last component of the path, i.e. 30.
# 3  
Old 10-21-2008
awesome, thank you! There went 45 min of my life that I will never get back, hah!
# 4  
Old 10-21-2008
Or:

Code:
filename=`echo ${filepath##*/}`

# 5  
Old 10-21-2008
Quote:
Originally Posted by Franklin52
Or:

Code:
filename=`echo ${filepath##*/}`

I would not use that trick in good conscience, those bash parameter substitution tricks are nice in the absence of any other tool (read sed, awk, basename), but they are cryptic, hard to read and to remember what they mean. I pity the person who would have to maintain a bash program that utilizes parameter substitution extensively.
# 6  
Old 10-21-2008
Quote:
Originally Posted by neked
I would not use that trick in good conscience, those bash parameter substitution tricks are nice in the absence of any other tool (read sed, awk, basename), but they are cryptic, hard to read and to remember what they mean. I pity the person who would have to maintain a bash program that utilizes parameter substitution extensively.
This is not bash but POSIX.

Consider the following:

Code:
$ s="/a/b/c"
$ time (for((i=1;i<=200;i++));do  s=${s##*/};done)

real    0m0.018s
user    0m0.016s
sys     0m0.000s
$ time (for((i=1;i<=200;i++));do  s=$(basename "$s");done)

real    0m4.611s
user    0m6.831s
sys     0m2.515s


Last edited by radoulov; 10-21-2008 at 03:41 PM..
# 7  
Old 10-21-2008
Quote:
Originally Posted by radoulov
This is not bash but POSIX.

Consider the following:

Code:
$ s="/a/b/c"
$ time (for((i=1;i<=200;i++));do  s=${s##*/};done)

real    0m0.018s
user    0m0.016s
sys     0m0.000s
$ time (for((i=1;i<=200;i++));do  s=$(basename "$s");done)

real    0m4.611s
user    0m6.831s
sys     0m2.515s

Interesting, I stand corrected. I revise my earlier statement to say: those POSIX parameter substitution tricks are nice in the absence of any other tool (read sed, awk, basename) or the presence of a need for optimization. Although python is much slower than C/C++, I would use python over C/C++ anyday, unless there is a compelling reason to invest the extra developer hours to squeeze out the extra CPU seconds.

The other day I spent close to 20 minutes debugging a script riddled with those parameter substitution tricks, for a script that is run once every night and finishes in few milliseconds, so no obvious optimization needs. I estimate I would've spent closer to 5 minutes debugging if the author of that used sed instead.

Last edited by neked; 10-21-2008 at 03:57 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy filepath along with filename at end of another file

Hi , Please help me out for the below problem - I have 2 files in a directory - $ ls -ltr total 4 -rwx------+ 1 abc Domain Users 615 May 31 17:33 abc.txt -rwx------+ 1 abc Domain Users 0 May 31 17:33 ll.sh I want to write the filename of abc.txt along with the directory to the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

2. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

3. Shell Programming and Scripting

Appending sequence number at the end of file name

hi team, i need a script for renaming a file with sequence number. script get a file from one directory'/home/billing/Cmm/sms/sms_tmp' append sequence no at the end of file name and move a file to other directory/home/billing/Cmm/sms/. actual file is cdr201508271527 file after renaming ... (3 Replies)
Discussion started by: mfaizan40
3 Replies

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

5. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

6. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: rethink
7 Replies

7. Shell Programming and Scripting

To add a number at the end of the line

Hi Folks, Using the Vi, how can I add a numbers at the end of the line. For eg: I have the numbers in the file as: 58.125.33 22.58.68 25.144.225 114.25.38 I need to add .0/8 at the end of all the line. So, it should be like 58.125.33.0/8 22.58.68.0/8 25.144.225.0/8 114.25.38.0/8 (6 Replies)
Discussion started by: gsiva
6 Replies

8. Shell Programming and Scripting

How to find out if there's a number at the end of a var

Hi I want to find whether the argument passed to my script ends in a number or not. Like, I want to find out if the argument is of the form: xyzwpq123 or just of the form xyzwpq. Can someone please help me!? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

9. Shell Programming and Scripting

Number a list at end of line using 'sed'

Hi All I have a script which has produced a list, I have used 'sed' to number my list, but i want to list at end of line with the first line starting at zero (0) and brackets round it ie My List i want Hello (0) this (1) day (2) can (3) be (4) sed '/./=' filename | sed '/./N; s/\n/) /'... (5 Replies)
Discussion started by: chassis
5 Replies

10. Shell Programming and Scripting

Select matches between line number and end of file?

Hi Guys/Gals, I have a log file that is updated once every few seconds and I am looking for a way to speed up one of my scripts. Basically what I am trying to do is grep through a text file from start to finish once. Then each subsequent grep starts at the last line of the previous grep to... (4 Replies)
Discussion started by: Jerrad
4 Replies
Login or Register to Ask a Question