是否可以从文件1中提取2列,然后在文件2中找到它们,然后将相关的行从文件2中提取到文件3中?

时间:2018-11-20 23:04:58

标签: bash awk sed grep

我有2个文本文件。 File1有大约1,000行,而File2有20,000行。 File1的摘录如下:

 /BBC Micro/Thrust
 /Amiga/Alien Breed Special Edition '92
 /Arcade-Vertical/amidar
 /MAME (Advance)/mario
 /Arcade-Vertical/mspacman
 /Sharp X68000/Bubble Bobble (1989)(Dempa)
 /BBC Micro/Chuckie Egg

File2的摘录如下:

 005;005;Arcade-Vertical;;;;;;;;;;;;;;
 Alien Breed Special Edition '92;Alien Breed Special Edition '92;Amiga;;1992;Team 17;Action / Shooter;;;;;;;;;;
 Alien 8 (Japan);Alien 8 (Japan);msx;;1987;Nippon Dexter Co., Ltd.;Action;1;;;;;;;;;
 amidar;amidar;Arcade-Vertical;;;;;;;;;;;;;;
 Bubble Bobble (Japan);Bubble Bobble (Japan);msx2;;;;;;;;;;;;;;
 Buffy the Vampire Slayer - Wrath of the Darkhul King (USA, Europe);Buffy the Vampire Slayer - Wrath of the Darkhul King (USA, Europe);Nintendo Game Boy Advance;;2003;THQ;Action;;;;;;;;;;
 mario;mario;FBA;;;;;;;;;;;;;;
 mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
 Thrust;Thrust;BBC Micro;;;;;;;;;;;;;;
 Thunder Blade (1988)(U.S. Gold)[128K];Thunder Blade (1988)(U.S. Gold)[128K];ZX Spectrum;;;;;;;;;;;;;;
 Thunder Mario v0.1 (SMB1 Hack);Thunder Mario v0.1 (SMB1 Hack);Nintendo NES Hacks 2;;;;;;;;;;;;;;
 Thrust;Thrust;Vectrex;;;;;;;;;;;;;;

在File3(输出文件)中,使用grep,sed,awk或bash脚本,我想要实现以下输出:

  Thrust;Thrust;BBC Micro;;;;;;;;;;;;;;
  Alien Breed Special Edition '92;Alien Breed Special Edition '92;Amiga;;1992;Team 17;Action / Shooter;;;;;;;;;;
  amidar;amidar;Arcade-Vertical;;;;;;;;;;;;;;
  mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;; 

这与我之前问过的问题类似,但不同。我特别想避免Thrust; Thrust; Vectrex ;;;;;;;;;;;;;被记录在文件3中。

使用sudo awk -F \; 'NR == FNR {a [$ 1] = $ 0; next} $ 1 in {print a [$ 1]}'”,我发现Thrust; Thrust; Vectrex ;;;;;;;;;;;;记录在文件3中,而不是Thrust; Thrust; BBC Micro ;;;;;;;;;;;;; (后者是我正在寻找的输出)。

同样,mario; mario; FBA ;;;;;;;;;;;;;;不会出现在File3中,因为它与/ MAME(Advance)/ mario不匹配,因为“ MAME(Advance)”不匹配。那很好。泡泡龙(日本);泡泡龙(日本); msx2 ;;;;;;;;;;;;;与“ Sharp X68000”或“ Bubble Bobble(1989)(Dempa)”都不匹配。

1 个答案:

答案 0 :(得分:3)

使用AWK和关联数组,您可以使用以下方法:

<Target Name="CoverageReport" AfterTargets="CopySqlFiles" Condition="$(Configuration) == Integration">
    <Exec Command="..\packages\JetBrains.dotCover.CommandLineTools.2018.1.3\tools\dotCover.exe analyse /TargetExecutable=..\packages\NUnit.ConsoleRunner.3.8.0\tools\nunit3-console.exe /ReturnTargetExitCode /TargetArguments=&quot;$(TargetPath)&quot; /Filters=-:nunit.framework;-:IntegrationProjectTest;-:type=MyNamespace.View.*;-:type=*Test /TargetWorkingDir=$(TargetDir) /Output=$(TargetDir)\TestResult\MyCoverageReport.html /ReportType=HTML" />
</Target>

输出:

awk '
BEGIN {
  if ( ARGC != 3 ) exit(1);
  FS="/";
  while ( getline < ARGV[2] ) mfggames[$2"/"$3]=1;
  FS=";";
  ARGC=2;
}
mfggames[$3"/"$1]
' file2 file1

按文件1解决方案排序(根据评论请求):

Alien Breed Special Edition '92;Alien Breed Special Edition '92;Amiga;;1992;Team 17;Action / Shooter;;;;;;;;;;
amidar;amidar;Arcade-Vertical;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
Thrust;Thrust;BBC Micro;;;;;;;;;;;;;;

输出:

awk '
BEGIN {
  if ( ARGC != 3 ) exit(1);
  FS="/";
  while ( getline < ARGV[2] ) mfggames[$2"/"$3]=++order;
  FS=";";
  ARGC=2;
}
mfggames[$3"/"$1] { print(mfggames[$3"/"$1] FS $0); }
' file2 file1 | sort -n | cut -d ';' -f 2-