LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   string manipulation in BASH (http://www.linuxquestions.org/questions/programming-9/string-manipulation-in-bash-546429/)

ovince 04-16-2007 05:46 AM

string manipulation in BASH
 
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

acid_kewpie 04-16-2007 06:23 AM

i would do something like:
Code:

# echo join5x_angle_box35_tri.dat | sed -e "s/^.*box\(.*\)_.*$/\1/g"
35


_john_i_ 04-16-2007 08:02 AM

Here's another way:


Code:

echo 'join5x_angle_box35_tri.dat' | tr "nx_" "  " | awk '{print $2" "$6}'

5 35


ghostdog74 04-16-2007 11:07 AM

Code:

echo "join5x_angle_box35_tri.dat" | awk '{gsub(/[a-z_.]/," "); print $1,$2}'

cfaj 04-16-2007 07:15 PM

Quote:

Originally Posted by ovince
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
....


Code:

file=join5x_angle_box5_tri.dat

temp=${file#*_box}
num=${temp%%_*}



All times are GMT -5. The time now is 04:23 AM.