我想创建一个循环,将采取我的文件扩展名之一.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并已删除的东西为好电源。