开发者

assign a portion of a word to a variable

I am working on a bash script.

grep -R -l "image17" *

image17 will change to some other number when I go through my loop. When I execute the grep above, I get back the following:

slides/_rels/slide33.xml.rels

I need to put slide33 in a variable because I want to use that to rename the file named image17.jpeg to be called slide33.jpeg. I need something to check for the above format and parse out starting at slide and ending with the numbers.

Another problem is the grep statement could come up开发者_StackOverflow with multiple results rather than one. I need a way to check to see how many results and if one do one thing and if more than one do another.

Here is what I have so far. Now I just need to put the grep as a variable and check to see how many times it happens and if it is one then do the regular expression to get the filename.

#!/bin/sh IFS=$'\n' 
where="/Users/mike/Desktop/test"
cd "${where}"
for file in $(find * -maxdepth 0 -type d)
do 
cd "${where}/${file}/images" 
ls -1 | grep -v ".png" | xargs -I {} rm -r "{}" 
cd "${where}/${file}/ppt" 
for images in $(find * -maxdepth 0 -type f) 
do 
if [ (grep -R -l "${images}" * | wc -l) == 1 ]
then
new_name=grep -R -l "slide[0-9]"
fi

done 
done


i=0
while [ $i -lt 50 ]
  do
    grep -R -l "image${i}"
done

something like this might help

Or, to detect similar structured words you can do

grep -R -l "slide[0-9][0-9]"

or you can do

grep -R -l "slide[0-9]+"

to match atleast one digit and atmost any number

Check man grep for more in the "REGULAR EXPRESSION" section

this will match words starting with "slide" and ending with exactly two numbers

grep -c does count the number of matches, but does not print the matches. I think you should count the lines to detect the number of lines which grep matched and then execute the conditional statement.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜