开发者

selecting the text string

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


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}'


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|'


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


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜