PDA

View Full Version : How to Match This?



huangyingw
March 30th, 2009, 02:41 PM
hello:
I met difficulty when trying regular expression in Ubuntu. The original string is a file with the full path name:'/media/storage/primaryStorage/dvd\ disk/temp.sh', I want to use grep to match out only the file name: 'temp.sh'.
How could I realize this? I found that, in grep, it does not support *,. [],etc.In general, the grep, and sed, seem to only support simple regular expression.

dwhitney67
March 30th, 2009, 02:54 PM
Try using basename.

Example:


basename /media/storage/primaryStorage/dvd\ disk/temp.sh

ghostdog74
March 30th, 2009, 02:56 PM
you are not very clear. you have a string "/media/storage/primaryStorage/dvd\ disk/temp.sh" stored in a variable, and you want to get the last part of it after the last "/", ie temp.sh?


# str="/media/storage/primaryStorage/dvd\ disk/temp.sh"
# echo ${str##*/}
temp.sh

huangyingw
March 30th, 2009, 03:50 PM
thank you all, even with such an unclear discription.
To make it clear.
sh file:
================================================== ===============
#! /bin/sh
egrep -o '[^/]+[^1234567890]*[.]jpg' /root/myproject/linux/shell/regulator/file
================================================== ===============
file content
================================================== ===============
/media/storage/aidafe03a004.jpg
================================================== ===============
my expecting result:
aidafe
I first need aidafe03a004.jpg, then, I want to delete the number, and .jpg post fix. So, my final target is aidafe.

huangyingw
March 30th, 2009, 04:00 PM
while, I found this expression does not work:
egrep -o '[^/]+[^1234567890]*[.](?=jpg)' /root/myproject/linux/shell/regulator/file
It seems that (?=xxx), does not work in egrep. This is my confusion.

rolandrock
March 30th, 2009, 07:12 PM
Don't know about egrep, you cuold try using sed. Depends on exact filename spec but a place to start:

sed -n 's,.*/\([a-z]*\).*\.jpg,\1,p' $filename

ghostdog74
March 31st, 2009, 01:59 AM
while read line
do
line=${line##*/}
echo ${line%%[0-9]*jpg}
done < file

huangyingw
March 31st, 2009, 03:03 PM
Don't know about egrep, you cuold try using sed. Depends on exact filename spec but a place to start:

sed -n 's,.*/\([a-z]*\).*\.jpg,\1,p' $filename
Great, this works for my case, Thanks you.

huangyingw
March 31st, 2009, 03:04 PM
while read line
do
line=${line##*/}
echo ${line%%[0-9]*jpg}
done < file

Thank you a lot, though I still prefer to realize this in regular expression.

rolandrock
March 31st, 2009, 05:15 PM
Great, this works for my case, Thanks you.

Glad I could help. BTW, if you need to match upper and lower case text, change the 'z' to 'Z'.

huangyingw
April 1st, 2009, 03:48 AM
Glad I could help. BTW, if you need to match upper and lower case text, change the 'z' to 'Z'.
Hi, in fact, I only have a rough understanding at your sed expression. Could you guid me to a URL or pdf, that could give me detail introduction about sed.