Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-16-2007, 05:46 AM
|
#1
|
|
Member
Registered: Mar 2007
Posts: 77
Rep:
|
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
|
|
|
|
04-16-2007, 06:23 AM
|
#2
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
i would do something like:
Code:
# echo join5x_angle_box35_tri.dat | sed -e "s/^.*box\(.*\)_.*$/\1/g"
35
|
|
|
|
04-16-2007, 08:02 AM
|
#3
|
|
Member
Registered: Aug 2003
Location: Austin, TX
Distribution: Linux from Scratch
Posts: 52
Rep:
|
Here's another way:
Code:
echo 'join5x_angle_box35_tri.dat' | tr "nx_" " " | awk '{print $2" "$6}'
5 35
|
|
|
|
04-16-2007, 11:07 AM
|
#4
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Code:
echo "join5x_angle_box35_tri.dat" | awk '{gsub(/[a-z_.]/," "); print $1,$2}'
|
|
|
|
04-16-2007, 07:15 PM
|
#5
|
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221
Rep:
|
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%%_*}
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:35 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|