将单词的一部分分配给变量
我正在编写 bash 脚本。
grep -R -l "image17" *
当我执行循环时,image17 将更改为其他数字。当我执行上面的 grep
时,我得到以下结果:
slides/_rels/slide33.xml.rels
我需要将 slide33
放入变量中,因为我想用它来重命名名为 image17 的文件.jpeg
被称为 slide33.jpeg
。我需要一些东西来检查上述格式并解析从幻灯片开始到数字结束的内容。
另一个问题是 grep 语句可能会产生多个结果而不是一个。我需要一种方法来检查有多少结果,以及是否有一个人做一件事,以及是否有多个人做另一件事。
这是我到目前为止所拥有的。现在我只需要将 grep 作为变量并检查它发生了多少次,如果是一次,则执行正则表达式来获取文件名。
#!/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 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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似的东西可能会有所帮助
或者,要检测类似的结构化单词,您可以执行
或可以执行
匹配至少一位数字和至多任何数字的操作,
请在“正则表达式”部分中查看
man grep
以获取更多信息,这将匹配以“slide”开头并以两个数字结尾的单词
grep -c
会计算匹配项的数量,但不会打印匹配项。我认为你应该计算行数来检测 grep 匹配的行数,然后执行条件语句。something like this might help
Or, to detect similar structured words you can do
or you can do
to match atleast one digit and atmost any number
Check
man grep
for more in the "REGULAR EXPRESSION" sectionthis 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 whichgrep
matched and then execute the conditional statement.