LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 04-16-2007, 06:46 AM   #1
ovince
Member
 
Registered: Mar 2007
Posts: 77

Rep: Reputation: 15
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
 
Old 04-16-2007, 07:23 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
i would do something like:
Code:
# echo join5x_angle_box35_tri.dat | sed -e "s/^.*box\(.*\)_.*$/\1/g"
35
 
Old 04-16-2007, 09:02 AM   #3
_john_i_
Member
 
Registered: Aug 2003
Location: Austin, TX
Distribution: Linux from Scratch
Posts: 52

Rep: Reputation: 15
Here's another way:


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

5 35
 
Old 04-16-2007, 12:07 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
echo "join5x_angle_box35_tri.dat" | awk '{gsub(/[a-z_.]/," "); print $1,$2}'
 
Old 04-16-2007, 08:15 PM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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%%_*}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Manuel Alvarado Linux - Newbie 2 09-06-2006 10:49 PM
String manipulation with BASH script King V Programming 9 04-21-2006 04:15 PM
string manipulation mannahazarika Programming 6 12-31-2005 12:02 PM
bash + string manipulation dave bean Programming 7 02-16-2005 12:16 PM
String manipulation with a script? philipina General 1 03-15-2004 01:07 PM


All times are GMT -5. The time now is 07:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration