LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-19-2007, 02:59 PM   #1
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Rep: Reputation: 32
BASH RegEx file name parsing


Hello all.

I'm uncertain of how to do this with Regular Expressions and BASH. I have a long filename and a regex that matches part of the file name. How do i store the part of the file name i want?

Heres is some psuedo code that kind of explains what i want:

fileName="/home/user1/1234file.txt"

fileVersion=`regex "[0-9]{4}" $fileName`


Any thoughts?

-s
 
Old 04-19-2007, 03:14 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
try this... fileversion=$(echo $filename | sed s/^.*\([0-9]{4}\).*$/\1/g)
 
Old 04-19-2007, 04:23 PM   #3
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Original Poster
Rep: Reputation: 32
Thanks for the response.

Sadly, that gives me the whole line, not just the part that i'm looking for.
Desired result:
$ echo $fileversion
1234

With "echo $(echo $filename | sed s/^.*\([0-9]{4}\).*$/\1/g)"
$ echo $fileversion
/home/user1/1234file.txt

I follow your logic using sed, but i am not very sed savy to explain why its not working for me.
 
Old 04-19-2007, 04:59 PM   #4
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Original Poster
Rep: Reputation: 32
figured it out.

the regex was incompatible with sed. '{x}' does not repeat the previous pattern x times. its literally interpreted. In addition, with out the -n option sed displays all input. So sed wasn't making a match and was displaying all input lines.

This works:
echo $(echo $filename | sed s/^.*\([0-9][0-9][0-9][0-9]\).*$/\1/g)

And for anyone whos interested a short sed tutorial can be found at:
http://www.cs.hmc.edu/tech_docs/qref/sed.html
 
Old 04-19-2007, 05:04 PM   #5
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Original Poster
Rep: Reputation: 32
As there is usually more than one way to skin a cat; is there a way to do this
1. in 1 line, like the way we did it w/ sed
2. have a more robust regex (i.e. have things like {x} at one's disposal)
 
Old 04-19-2007, 05:05 PM   #6
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Original Poster
Rep: Reputation: 32
i suppose i could stop bash scripting and start perl scripting...
 
Old 04-27-2007, 05:13 PM   #7
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Hewson
As there is usually more than one way to skin a cat; is there a way to do this
1. in 1 line, like the way we did it w/ sed
2. have a more robust regex (i.e. have things like {x} at one's disposal)

i could have used grep -e (which is supposed to be the same as egrep, but some report egrep to be a crippled version of grep -e)

this would have worked as well:
echo $(echo $filename | grep -oE "[0-9]{4}")

certainly less messy.
 
Old 04-27-2007, 05:37 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,126

Rep: Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985
Quote:
Originally Posted by Hewson
i could have used grep -e (which is supposed to be the same as egrep, but some report egrep to be a crippled version of grep -e)
Interesting - in my ignorance I use them interchangably.
And yes, perl is another option.
 
  


Reply

Tags
bash, grep, regex, sed


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
Help on parsing a log file in BASH globemast Programming 5 01-11-2007 01:56 AM
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM
parsing standard input with bash arosales Programming 3 07-16-2005 10:42 AM
bash script, parsing email addresses kepler Programming 6 01-26-2004 06:47 AM


All times are GMT -5. The time now is 11:27 AM.

Main Menu
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