LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2014, 07:34 PM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
Question extract required string from the file name


How to extract the required string "the string part that contains D and ends in "." " from the following strings.

Suppose the test strings are:

1. X.X999.XML.JAVAEE.D02122014.T1026.TXT
2. X.X888.XML.JAVAEB.D02082014.T1026
3. X.X909.XML.JAVAEE.D01232014.T0926.MP4

I have found a way to do it but do not know how to do it using shell script.

Extract the string part that occurs between 4th and 5th "." from the beginnig of the string.

Thanks in advance!!!
 
Old 02-01-2014, 07:53 PM   #2
Isaac Velando
LQ Newbie
 
Registered: Feb 2014
Location: Texas
Distribution: Arch, Ubuntu Server, CentOS
Posts: 29

Rep: Reputation: 21
If you're looking to extract the substrings from file names, try something like this:

find /path/to/files -maxdepth 1 -type f -regex '^.+\.D[0-9]+\..+$' | egrep -o 'D[0-9]+'

This should work assuming all the files are formatted like you listed, but if not let me know. If you'd like any of the regular expressions or options explained let me know as well.
 
Old 02-02-2014, 01:26 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
How about you show your solution and advise where your stuck on placing it in a script?
 
Old 02-02-2014, 01:58 AM   #4
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
I've not designed any solution for it only found out the algorithm to extract the required string.

Thanks!
 
Old 02-02-2014, 04:45 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
So where are you stuck? If you can extract the string one at a time, you would need to loop over the file names to complete the task for each.

Remember, we are here to assist when you are stuck, not to do the work for you.
 
Old 02-04-2014, 03:02 AM   #6
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Hello all:

Can we use sed for this task?
 
Old 02-04-2014, 03:10 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
$ cat file
X.X999.XML.JAVAEE.D02122014.T1026.TXT
X.X888.XML.JAVAEB.D02082014.T1026
X.X909.XML.JAVAEE.D01232014.T0926.MP4
$ sed -r 's/^([^.]+\.){4}(D[^.]+\.).*$/\2/' file
D02122014.
D02082014.
D01232014.
 
1 members found this post helpful.
Old 02-04-2014, 08:25 AM   #8
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
Quote:
Extract the string part that occurs between 4th and 5th "." from the beginnig of the string.
Is the 'dot' important?
Code:
$ cat file
1. X.X999.XML.JAVAEE.D02122014.T1026.TXT
2. X.X888.XML.JAVAEB.D02082014.T1026
3. X.X909.XML.JAVAEE.D01232014.T0926.MP4
$ cut -d "." -f 6 file
D02122014
D02082014
D01232014

Last edited by allend; 02-04-2014 at 08:55 AM.
 
1 members found this post helpful.
Old 02-04-2014, 08:53 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
awk -F. ' { print $5 } ' file
should work also, and:
Code:
a=X.X999.XML.JAVAEE.D02122014.T1026.TXT
b="D${a#*.D}"
echo ${b%%.*}
will work too
 
  


Reply

Tags
regular expression


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
how to extract from a string Owais.Ahmad Linux - Newbie 13 07-27-2012 07:13 AM
[SOLVED] How to extract string with exact case from a file? guest Programming 16 08-30-2011 02:52 PM
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 11:44 PM
Extract value from string ewingtux Programming 1 12-27-2008 08:19 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 10:43 AM


All times are GMT -5. The time now is 08:03 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration