I have some files that have names like this: 'abcdefg_y_zz.jpg' The 'abcdefg' is a sequence of digits, while the 'y' and 'zz' are letters. I need to get all the files that have the sequence of digits ending with a number greater than 10. The files that have 'fg' greater that 10.
我有一些文件名如下:'abcdefg_y_zz.jpg''abcdefg'是一个数字序列,而'y'和'zz'是字母。我需要获得所有具有以大于10的数字结尾的数字序列的文件。具有大于10的'fg'的文件。
Does anyone have an idea on how to do that in a bash script?
有没有人知道如何在bash脚本中执行此操作?
6 个解决方案
#1
2
Ok, technically, based on all your info...
好的,从技术上讲,基于你的所有信息......
ls | grep '[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg'
ls | grep'[0-9] {5} [1-9] [0-9] _ [[:alpha:]] _ [[:alpha:]] {2} .jpg'
#2
1
How about this? Just exclude ones which have 0 in position f.
这个怎么样?只要排除位置f为0的那些。
ls -1 | grep -v "?????0?_?_??.jpg"
Update Since you want > 10 and not >= 10, you'll need to exclude 10 too. So do this:
更新既然您想要> 10而不是> = 10,那么您也需要排除10。这样做:
ls -1 | grep -v "?????0*_?_??.jpg" | grep -v "??????10_?_??.jpg"
#3
0
with more scripting
更多脚本
#!/bin/bash
for i in seq FIRST INCREMENT LAST
do
cp abcde$i_y_zz.jpg /your_new_dir //or whatever you want to do with those files
done
so in your example line with seq will be
所以在您的示例中,seq将会是
for i in seq 11 1 100000000
#4
0
If the filenames are orderly named this awk
solution works:
如果文件名按顺序命名,则此awk解决方案有效:
ls | awk 'BEGIN { FIELDWIDTHS = "5 2" } $2 > 10'
Explanation
-
FIELDWIDTHS = "5 2"
means that$1
will refer to the first 5 characters and$2
the next 2. - FIELDWIDTHS =“5 2”表示$ 1将引用前5个字符,$ 2表示接下来的2个字符。
-
$2 > 10
matches when field 2 is greater than 10 and implicitly invokes the default code block, i.e.'{ print }'
- 字段2大于10时$ 2> 10匹配并隐式调用默认代码块,即'{print}'
#5
0
Just one process:
只有一个过程:
ls ?????+(1[1-9]|[2-9]?)_?_??.jpg
#6
0
All the solutions provided so far are fine, but anybody who's had some experience with shell programming knows that parsing ls
is never a good idea and must be avoided. This actually doesn't apply in this case, where we can assume that the names of the files follow a certain pattern, but it's a rule that should be remembered. More explanation here.
到目前为止提供的所有解决方案都很好,但是任何有shell编程经验的人都知道解析ls永远不是一个好主意,必须避免。这实际上不适用于这种情况,我们可以假设文件的名称遵循某种模式,但这是一个应该被记住的规则。这里有更多解释。
What you want can be achieved much safer with GNU find
- assuming that you run the command in the directory where the files are, it would look something like this :
使用GNU find可以实现您想要的更安全 - 假设您在文件所在的目录中运行命令,它看起来像这样:
find . -regextype posix-egrep -regex '\./[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg$'
#1
2
Ok, technically, based on all your info...
好的,从技术上讲,基于你的所有信息......
ls | grep '[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg'
ls | grep'[0-9] {5} [1-9] [0-9] _ [[:alpha:]] _ [[:alpha:]] {2} .jpg'
#2
1
How about this? Just exclude ones which have 0 in position f.
这个怎么样?只要排除位置f为0的那些。
ls -1 | grep -v "?????0?_?_??.jpg"
Update Since you want > 10 and not >= 10, you'll need to exclude 10 too. So do this:
更新既然您想要> 10而不是> = 10,那么您也需要排除10。这样做:
ls -1 | grep -v "?????0*_?_??.jpg" | grep -v "??????10_?_??.jpg"
#3
0
with more scripting
更多脚本
#!/bin/bash
for i in seq FIRST INCREMENT LAST
do
cp abcde$i_y_zz.jpg /your_new_dir //or whatever you want to do with those files
done
so in your example line with seq will be
所以在您的示例中,seq将会是
for i in seq 11 1 100000000
#4
0
If the filenames are orderly named this awk
solution works:
如果文件名按顺序命名,则此awk解决方案有效:
ls | awk 'BEGIN { FIELDWIDTHS = "5 2" } $2 > 10'
Explanation
-
FIELDWIDTHS = "5 2"
means that$1
will refer to the first 5 characters and$2
the next 2. - FIELDWIDTHS =“5 2”表示$ 1将引用前5个字符,$ 2表示接下来的2个字符。
-
$2 > 10
matches when field 2 is greater than 10 and implicitly invokes the default code block, i.e.'{ print }'
- 字段2大于10时$ 2> 10匹配并隐式调用默认代码块,即'{print}'
#5
0
Just one process:
只有一个过程:
ls ?????+(1[1-9]|[2-9]?)_?_??.jpg
#6
0
All the solutions provided so far are fine, but anybody who's had some experience with shell programming knows that parsing ls
is never a good idea and must be avoided. This actually doesn't apply in this case, where we can assume that the names of the files follow a certain pattern, but it's a rule that should be remembered. More explanation here.
到目前为止提供的所有解决方案都很好,但是任何有shell编程经验的人都知道解析ls永远不是一个好主意,必须避免。这实际上不适用于这种情况,我们可以假设文件的名称遵循某种模式,但这是一个应该被记住的规则。这里有更多解释。
What you want can be achieved much safer with GNU find
- assuming that you run the command in the directory where the files are, it would look something like this :
使用GNU find可以实现您想要的更安全 - 假设您在文件所在的目录中运行命令,它看起来像这样:
find . -regextype posix-egrep -regex '\./[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg$'