Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string manipulation in BASH

Status
Not open for further replies.

ovince

Programmer
Feb 27, 2007
55
FR
Hi All,

I would like to extract numbers 5, 15, 35 from filenames like (the second number in name):

join5x_angle_box5_tri.dat
join5x_angle_box15_tri.dat
join5x_angle_box35_tri.dat
....

How to do this?

I was thinking to apply command like


`expr index "$fileName" '.*box.*'`


in a loop, but it gives me back wrong position of the first number (2). What I do wrong here?

Thanks
oliver
 
Hi

[tt]expr index[/tt] does not search for substring, but for set of characters. So it does not return the starting position of "box" but the first position of any letter from the "b", "o", "x" set.
Code:
expr match "$fileName" '.*box'

Feherke.
 
thanks.

this works
expr match "$fileName" '.*box'


but how come that
expr match "$fileName" '.*_tri'

does not find the position of '_'(ie the position of the end of the number)?

o.
 
And if you insist on expr:
`expr "$filename" : '.*box\([0-9]*\)_tri*'`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top