以下是一些示例文件名:
1_0_1_2A34_8_70_1680000_0_0_0.png
1_0_1_1_1A2C_1_5A2F16_0_0_0.png
1_0_1_3EC_6_601_D7A0000_0_0_0.png
1_0_1_152D_7E3_2_11A2F26_0_0_0.png
首先,我试图让 linux 将文件名识别为由下划线“_”分隔的 10 列文本。
例如:
col1_col2_col3_col4_col5_col6_col7_col8_col9_col10.png
然后我想告诉 linux 在文件名的第 7 列的前 3 个字符中搜索字符串(例如“11A”)。
我有数千个这样的文件,如果我定期搜索“11A”,我会找到包含 11A 但位于第 4、5 和 7 列的文件的匹配项。
最佳答案
这是一个 Perl 脚本(我们称之为 match7th.pl
),它将打印出第 7 个字段与给定模式匹配的任何行(例如 11A
):
#!/usr/bin/perl -w
use strict;
my $pat = qr/$ARGV[0]/;
while (<STDIN>) {
my (undef, undef, undef, undef, undef, undef, $seventh) = split /_/;
print if defined($seventh) && $seventh =~ $pat;
}
像这样运行它:
find . -name '*.png' | perl match7th.pl 11A
关于linux - 在 Linux 中搜索文件名中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18477628/