将单词的一部分分配给变量

发布于 2024-11-17 02:26:32 字数 889 浏览 2 评论 0原文

我正在编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-11-24 02:26:32
i=0
while [ $i -lt 50 ]
  do
    grep -R -l "image${i}"
done

类似的东西可能会有所帮助

或者,要检测类似的结构化单词,您可以执行

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

或可以执行

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

匹配至少一位数字和至多任何数字的操作,

请在“正则表达式”部分中查看 man grep 以获取更多信息,

这将匹配以“slide”开头并以两个数字结尾的单词

grep -c 会计算匹配项的数量,但不会打印匹配项。我认为你应该计算行数来检测 grep 匹配的行数,然后执行条件语句。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文