我有一个文件夹结构等:grep的文件夹名和子文件内容
debug/$domain/info.txt
在调试是像200个域
,我想到grep的info.txt
文件的每个域的特定内容 所以我想记下域名+我需要grep的info.txt
的内容部分。
我尝试了很多东西,但我失败了。
for D in $(find . -type d); do
grep xxx D/info.txt
done
如果您有任何想法如何做,请让我知道。
谢谢:)
我有一个文件夹结构等:grep的文件夹名和子文件内容
debug/$domain/info.txt
在调试是像200个域
,我想到grep的info.txt
文件的每个域的特定内容 所以我想记下域名+我需要grep的info.txt
的内容部分。
我尝试了很多东西,但我失败了。
for D in $(find . -type d); do
grep xxx D/info.txt
done
如果您有任何想法如何做,请让我知道。
谢谢:)
有您的正则表达式部分(查找内容)已经完成,尝试这样的事情:
while IFS= read -r -d $'\0'; do
domain="${$REPLY##*/}"
content="$(grep -o xxx $REPLY/info.txt)"
echo "$domain: $content" >> log.txt
done < <(find . -type d -print0)
或使用for循环的尝试:
for D in $(find . -type d); do
content="$(grep -o xxx D/info.txt)"
domain="$D##*/"
echo "$domain: $content" >>log.txt
done
虽然请记住,这个for循环是而不是空白区域安全,但对于这种特殊情况无关紧要。
下面的脚本是这样做的另一种方式:
find /path/to/search/for -type f -name "*info.txt" -print0 | while read -r -d '' line
do
domain=$(sed 's/^.*debug\/\(.*\)\/info.txt/\1/' <<<"$line")
content=$(grep "text_to_grab" "$line")
printf "%s : %s\n" "$domain" "$content" >>logfile
done
因为在您添加标记的perl你的问题,我公司提供使用Perl的解决方案。
use strict;
use diagnostics;
my $search_for = qr{abc}; #string to search for
print search_info_files($search_for);
sub search_info_files {
my $rex = shift;
my $file_name = 'info.txt';
chdir 'debug' or die "Unable to chdir to debug: $!\n";
my @domains = glob("*");
foreach my $domain (@domains) {
next unless -d $domain;
next unless -f $domain . '/' . $file_name;
open my $fh, '<', $domain . '/' . $file_name
or die "Unable to open $domain/$file_name: $!\n";
while (<$fh>) {
chomp(my $line = $_);
next unless $line =~ $rex;
print "'$domain' matches (line#: $.): $line.\n";
}
close $fh;
}
}
__END__
Sample output:
'a' matches (line#: 1): As easy as abc.
'b' matches (line#: 2): abcde.
'c' matches (line#: 1): abcde.
'c' matches (line#: 3): abcde.
'c' matches (line#: 5): Sometimes more than one line contains abc.
'd' matches (line#: 1): abcde.
'd' matches (line#: 3): abcde.
'e' matches (line#: 1): abcde.
例如调试/ C/info.txt包含:需要
abcde fghij abcde fffff Sometimes more than one line contains abc
与内容info.txt的一部分,建立一个正则表达式... – Jahid
欢迎SO ,请展示您的编码工作。 – Cyrus
内容的正则表达式已完成。只是伪装的东西。 – user5293028