我想創建一個循環,將採取我的文件擴展名之一.tar.gz 解壓縮它解壓縮並使用grep搜索裏面的文件(擴展名爲.tlg) a >> output.text。幫助與unix焦油和grep循環
在outout.text我需要匹配的數據以及從
一個這樣的搜索已經完成我想untared要刪除的文件和附帶的文件和家長焦油的名字預處理以繼續下一個tar文件,直到檢查完所有的焦油。
我不能解壓所有在一個爲我沒有磁盤空間用於此
誰能幫助 ?
謝謝
我想創建一個循環,將採取我的文件擴展名之一.tar.gz 解壓縮它解壓縮並使用grep搜索裏面的文件(擴展名爲.tlg) a >> output.text。幫助與unix焦油和grep循環
在outout.text我需要匹配的數據以及從
一個這樣的搜索已經完成我想untared要刪除的文件和附帶的文件和家長焦油的名字預處理以繼續下一個tar文件,直到檢查完所有的焦油。
我不能解壓所有在一個爲我沒有磁盤空間用於此
誰能幫助 ?
謝謝
爲避免創建臨時文件,可以使用GNU tar的--to-stdout
選項。
下面的代碼是小心在路徑空格和其他字符,可能混淆的殼:
#! /usr/bin/perl
use warnings;
use strict;
sub usage { "Usage: $0 pattern tar-gz-file ..\n" }
sub output_from {
my($cmd,@args) = @_;
my $pid = open my $fh, "-|";
warn("$0: fork: $!"), return unless defined $pid;
if ($pid) {
my @lines = <$fh>;
close $fh or warn "$0: $cmd @args exited " . ($? >> 8);
wantarray ? @lines : join "" => @lines;
}
else {
exec $cmd, @args or die "$0: exec $cmd @args: $!\n";
}
}
die usage unless @ARGV >= 2;
my $pattern = shift;
foreach my $tgz (@ARGV) {
chomp(my @toc = output_from "tar", "-ztf", $tgz);
foreach my $tlg (grep /\.tlg\z/, @toc) {
my $line = 0;
for (output_from "tar", "--to-stdout", "-zxf", $tgz, $tlg) {
++$line;
print "$tlg:$line: $_" if /$pattern/o;
}
}
}
樣品運行:
$ ./grep-tlgs hello tlgs.tar.gz tlgs/another.tlg:2: hello tlgs/file1.tlg:2: hello tlgs/file1.tlg:3: hello tlgs/third.tlg:1: hello
$ ./grep-tlgs^tlgs.tar.gz tlgs/another.tlg:1: blah blah tlgs/another.tlg:2: hello tlgs/another.tlg:3: howdy tlgs/file1.tlg:1: whoah tlgs/file1.tlg:2: hello tlgs/file1.tlg:3: hello tlgs/file1.tlg:4: good-bye tlgs/third.tlg:1: hello tlgs/third.tlg:2: howdy
$ ./grep-tlgs^xtlgs.tar.gz tar: xtlgs.tar.gz: Cannot open: No such file or directory tar: Error is not recoverable: exiting now tar: Child returned status 2 tar: Exiting with failure status due to previous errors ./grep-tlgs: tar -ztf xtlgs.tar.gz exited 2 at ./grep-tlgs line 14.
你可以遍歷焦油,提取它們,然後grep他們;這樣的事情應該工作:
match="somestring"
mkdir out/
for i in *.tar.gz; do
mkdir out/${i} # create outdir
tar -C out/${i} -xf ${i} # extract to sub-dir with same name as tar;
# this will show up in grep output
cd out
grep -r ${match} ${i} >> ../output.text
cd ..
rm -rf out/${i} # delete untarred files
done
要小心,因爲$ i變量的內容傳遞給RM -rf並已刪除的東西爲好電源。