I have filenames like:
12345V001_Test1-Test2-Test3.jpg or 12345V001-Test1_Test2_Test3.tif
At the beginning there is always a number, which can also contain letters. This is followed by keywords separated by a hyphen or an underscore.
I need the number which is at the beginning of the file name. Here is my problem that it is not always consistently the same pattern. It can be followed by an underscore or a hyphen.
Example:
12345V001_Test1-Test2-Test3.jpg (extract before underscore) -> 12345V001
12345V001-Test1_Test2-Test3.jpg (extract before hyphen) -> 12345V001
CodePudding user response:
You can use sed
to parse the text with regexp, such as
echo '12345V001_Test1-Test2-Test3.jpg' | sed -E 's/^([^_-]*).*$/\1/g'
If you are listing the number of all files inside current working directory, you can simply pipe the filename from ls
ls | sed -E 's/^([^_-]*).*$/\1/g'
CodePudding user response:
Assuming that the filename is stored in a variable v
, you could get this by
prefix=${v%%[_-]*}