grep foldername and sub file content

时间:2016-05-15 18:08:03

标签: regex bash perl awk grep

i have a folder structure like:

debug/$domain/info.txt

in debug are like 200 domains

and i want to grep a specific content of the info.txt file for each domain so i want to log down the domain + a content part of the info.txt which i need to grep.

i tried much stuff but i failed.

for D in $(find . -type d); do
  grep xxx D/info.txt 
done

if you got any idea how to be done, please let me know.

thanks :)

3 个答案:

答案 0 :(得分:0)

正如你已经完成正则表达式部分(查找内容)一样,尝试这样的事情:

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循环是白色空间安全,但是对于这个特定情况并不重要。

答案 1 :(得分:0)

下面的脚本是另一种方法:

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

答案 2 :(得分:0)

由于您在标题中添加了标签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.

例如,debug / c / info.txt包含:

abcde
fghij
abcde
fffff
Sometimes more than one line contains abc