ABCDchinchwad18-Mar-2010-11.sql.zip
ABCDsolapur18-Mar-2010-10.sql.zip
How do I find the string between "ABCD" and the date "18-Mar-2010"
Expected resuts:
chinchwad
solapur
ABCDchinchwad18-Mar-2010-11.sql.zip
ABCDsolapur18-Mar-2010-10.sql.zip
How do I find the string between "ABCD" and the date "18-Mar-2010"
Expected resuts:
chinchwad
solapur
Maybe you should head to regexps
"ABCDchinchwad18-Mar-2010-11.sql.zip".match(/ABCD(\w+)[0-9]{2}-[a-zA-Z]{3}-[0-9]{4}/)[1]; //chinchwad "ABCDsolapur18-Mar-2010-10.sql.zip".match(/ABCD(\w+)[0-9]{2}-[a-zA-Z]{3}-[0-9]{4}/)[1]; // solapur
for file in ABCD*zip
do
file="${file/#ABCD/}"
echo ${file%%[0-9][0-9]-*-*}
done
or using sed
ls ABCD*zip | sed 's/^ABCD//;s/[0-9][0-9]-.*-.*//'
or using awk
ls ABCD*zip | awk -F"[0-9][0-9]-|ABCD" '{print $2}'
The filename contains the following input content
ABCDchinchwad18-Mar-2010-11.sql.zip
ABCDsolapur18-Mar-2010-10.sql.zip
sed -r 's/([A-Z]+)([a-z]+)(.*)/\2/' filename
The output is
chinchwad
solapur
Perl will do well here:
ls *.zip | perl -pe 's/ABCD (\w+) \d{2}-\w{3}-\d{4} .*/$1/x'
If you must use find
:
find . -maxdepth 1 -name \*.zip |
perl -pe 's/.* ABCD (\w+) \d{2}-\w{3}-\d{4} .*/$1/x'
sed 's/ABCD\(.*\)[0-9]\{2\}-[[:alpha:]].*.sql.zip/\1/'
Output:
chinchwad
solapur
pune2
To remove the path added by find
(which is more flexible, portable and maintainable than parsing ls
):
sed 's|.*/ABCD\(.*\)[0-9]\{2\}-[[:alpha:]].*.sql.zip|\1|'