Reputation: 847
with linux bash shell , how can i use regex to get a certain string of a file
by example:
for filename *.tgz do
"get the certain string of filename (in my case, get 2010.04.12 of file 2010.01.12myfile.tgz)"
done
or should I turn to perl
Merci
frank
Upvotes: 0
Views: 501
Reputation: 17335
#!/bin/sh
a="2010.04.18Myfile.tgz"
echo ${a%%+([a-zA-Z.])}
bash' regexp are quite powerful (at least compared to standard sh or command.com :-))
Upvotes: 0
Reputation: 342363
with bash, for the simplest case, if you know what you want to get is a date stamp, you can just use shell expansion
#!/bin/bash
for file in 20[0-9][0-9].[01][0-9].[0-9][0-9]*tgz
do
echo $file
done
else, if its anything before the first alphabet,
for file in *tgz
do
echo ${file%%[a-zA-Z]*}
done
otherwise, you should spell out your criteria for the search.
Upvotes: 1