(标题问题)
例如:
FILEA:
this is a
that was a
档案B:
that was a school
hi, you should ignore me
this is a example
that was a sample
...
我们想要的是在第三个文件中包含文件B的行1,3,4,(...)的文件。任何只有bash命令的解决方案?答案使用命令行" Perl" (如有必要)也可以提供帮助。
答案 0 :(得分:1)
使用awk你可以这样做:
awk 'NR==FNR{arr["^" $1]; next} {for (line in arr) if ($0 ~ line) {print; break}}' fileA fileB
that was a school
this is a example
that was a sample
首先,我们阅读fileA
并构建一个数组^
(行开头)与fileA
的每一行连接在一起。然后在浏览fileB
时,我们只需遍历arr
并打印匹配的任何行。
答案 1 :(得分:1)
使用命令行Perl:
$ perl -ne '
BEGIN {
local @ARGV = shift;
$pat = join "|", map {chomp; quotemeta} <>;
}
print if /$pat/
' filea fileb
输出:
that was a school
this is a example
that was a sample
答案 2 :(得分:1)
在Perl脚本中:
use warnings;
use strict;
my ($file1, $file2) = qw(file1.txt file2.txt);
open my $fh1, '<', $file1 or die "Can't open $file1: $!";
open my $fh2, '<', $file2 or die "Can't open $file2: $!";
my @compline = <$fh2>;
chomp @compline;
while (<$fh1>)
{
chomp $_;
foreach my $line (@compline)
{
if ($line =~ m/^$_/)
{
print "$line\n";
}
}
}
输出:
this is a example
that was a school
that was a sample