I've got this little script in sh
(Mac OSX 10.6) to look through an array of files. Google has stopped being helpful at this point:
files="*.jpg"
for f in $files
do
echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*'
name=$?
echo $name
done
So far (obviously, to you shell gurus) $name
merely holds 0, 1 or 2, depending on if grep
found that the filename matched the matter provided. What I'd like is to capture what's inside the parens ([a-z]+)
and store that to a variable.
I'd like to use grep
only, if possible. If not, please no Python or Perl, etc. sed
or something like it – I'm new to shell and would like to attack this from the *nix purist angle.
Also, as a super-cool bonus, I'm curious as to how I can concatenate string in shell? Is the group I captured was the string "somename" stored in $name, and I wanted to add the string ".jpg" to the end of it, could I cat $name '.jpg'
?
Thanks! And please explain what's going on, if you've got the time.