2016-05-15 10 views
-2

ich habe eine Ordnerstruktur wie:grep folder und Unterdateiinhalt

debug/$domain/info.txt

in debug wie 200 Domains sind

und ich will für jede Domäne einen bestimmten Inhalt der info.txt Datei grep also ich möchte die Domain + ein Inhalt Teil der info.txt, die ich grep muss abmelden.

Ich habe viel versucht, aber ich habe versagt.

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

Wenn Sie irgendeine Idee, wie getan werden, lassen Sie es mich bitte wissen.

Dank :)

+0

Teil info.txt mit dem Inhalt benötigt wird, eine Regex aufzubauen ... – Jahid

+2

Willkommen SO Bitte zeigen Sie Ihre Programmieranstrengungen. – Cyrus

+0

der Regex für den Inhalt ist fertig. einfach Pseudo-Zeug. – user5293028

Antwort

0

Wie Sie die Regex Teil (zum Auffinden von Inhalten) bereits getan haben, so etwas wie dies versuchen:

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) 

Oder Ihre for-Schleife Versuch mit:

for D in $(find . -type d); do 
    content="$(grep -o xxx D/info.txt)" 
    domain="$D##*/" 
    echo "$domain: $content" >>log.txt 
done 

Erinnern Sie sich, dass diese for-Schleife nicht Leerraum sicher ist, obwohl es für dieses spezielle Szenario nicht von Bedeutung ist.

0

Below Skript ist eine andere Art und Weise tun:

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 
0

Da Sie die Tag-perl zu Ihrer Frage hinzugefügt, biete ich eine Lösung mit 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. 

Zum Beispiel debug/c/info.txt enthält:

abcde 
fghij 
abcde 
fffff 
Sometimes more than one line contains abc