LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-26-2010, 05:46 AM   #1
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Rep: Reputation: 15
Extract part of file name


I have this string ./DAT000728-652523058.job.
I want to extract the no between DAT and - sign. I want 728. I dont want 000728.
as with
echo ./DAT000725-560162365.job | cut -d'T' -f2 | cut -d'-' -f1 I am getting 000728.

string can be ./DAT326822-652523058.job also. then i need 326822

How can I do that?

Last edited by himu3118; 03-26-2010 at 06:19 AM.
 
Old 03-26-2010, 05:54 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
echo "DAT000728-652523058.job" | sed 's/^[^[:digit:]]\+\(.*\)-.*/\1/'
 
Old 03-26-2010, 06:21 AM   #3
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
Thanx for the reply.

but its again giving 000728 but not 728
 
Old 03-26-2010, 06:34 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
This task can be done very fast (if not very elegantly) using Shell Parameter Expansion with the shell configured for "extended globbing"
Code:
c@CW8:~$ string='./DAT000728-652523058.job'
c@CW8:~$ shopt -s extglob
c@CW8:~$ string=${string##*([^0-9])}
c@CW8:~$ echo $string
000728-652523058.job
c@CW8:~$ string=${string%%-*}
c@CW8:~$ echo $string
000728
c@CW8:~$ string=${string##*(0)}
c@CW8:~$ echo $string
728
Alternatively, if the character positions are fixed and only 3 digits are required
Code:
c@CW8:~$ string='./DAT000728-652523058.job'
c@CW8:~$ number=${string:8:3}
c@CW8:~$ echo $number
 
Old 03-26-2010, 07:27 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
sed 's:.*DAT0*\([0-9]*\)-.*:\1:'

Code:
$ echo './DAT000728-6744645' | sed 's:.*DAT0*\([0-9]*\)-.*:\1:'
728
$ echo './DAT045728-6744645' | sed 's:.*DAT0*\([0-9]*\)-.*:\1:'
45728
 
1 members found this post helpful.
Old 03-26-2010, 07:46 AM   #6
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
thanx all for ur replies.

all the solutions above are working well with my script.
 
Old 03-26-2010, 08:52 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Is this not a repeat of this previous post??

http://www.linuxquestions.org/questi...script-797130/
 
  


Reply



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
How to extract a part of flat file in Shell Script? manickaraja Programming 8 02-16-2010 02:46 AM
Extract part of a string steven.c.banks Linux - General 7 05-07-2008 07:18 AM
How do I extract only part of a tarball? punt Linux - General 6 01-26-2006 01:57 AM
Extract the used part of dynamic library for my application??? okeyla Programming 1 12-21-2005 02:24 PM
How to extract a part of a line by sed? J_Szucs Programming 2 02-15-2003 06:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:30 AM.

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
Open Source Consulting | Domain Registration