tags:

views:

42

answers:

5

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

A: 

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
markcial
+1  A: 
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}'
ghostdog74
@ghostdog74, the OP has a new requirement in the comments above.
glenn jackman
A: 

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

muruga
# find . -maxdepth 1 -name "*.zip" | sed -r 's/([A-Z]+)([a-z]+)(.*)/\2/' ## ./chinchwad and ./solapur # how do remove the path?
shantanuo
@shantanuo, if you are using maxdepth 1, you might as well just do a listing using `ls`.
ghostdog74
A: 

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'
glenn jackman
using find has the recursive advantage, but using maxdepth 1 is the same as your `ls` version, except longer :)
ghostdog74
A: 
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|'
Dennis Williamson