LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   BASH RegEx file name parsing (http://www.linuxquestions.org/questions/linux-general-1/bash-regex-file-name-parsing-547407/)

Hewson 04-19-2007 02:59 PM

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

acid_kewpie 04-19-2007 03:14 PM

try this... fileversion=$(echo $filename | sed s/^.*\([0-9]{4}\).*$/\1/g)

Hewson 04-19-2007 04:23 PM

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.

Hewson 04-19-2007 04:59 PM

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

Hewson 04-19-2007 05:04 PM

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)

Hewson 04-19-2007 05:05 PM

i suppose i could stop bash scripting and start perl scripting...

Hewson 04-27-2007 05:13 PM

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.

syg00 04-27-2007 05:37 PM

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.


All times are GMT -5. The time now is 03:32 AM.