how to retrive every portion separately from following file name? DSA4020_frontcover_20346501_2011-05.doc
I want to retrieve informations as below; name = DSA4020
type = frontcover
id = 20346501
date = 2011-05
is it possible to do with sed??
feedback
|
|
How about this ' echo "DSA4020_frontcover_20346501_2011-05.doc" \ | sed 's|\([^_]*\)_\([^_]*\)_\([^_]*\)_\([^\.]*\).*|name=\1,type=\2,id=\3,date=\4|' gives this nice CSV form, name=DSA4020,type=frontcover,id=20346501,date=2011-05 | |||
|
feedback
|
|
You can use the following to split at every _ after removing the extension:
Replace the very last digit by 2, 3, 4 to get the individual value each time. | |||
|
feedback
|
|
If the number of fields is constant :
Then access ${VARS[i]}... Alternative :
And if supported (bash 3+)
If you have multiple filenames, just add a | |||||||
feedback
|
|
Split the filename and store in positional parameters:
| |||||||
feedback
|