是否可以使用Grep,Sed或Awk或bash脚本而不对输出文件进行排序?
发布时间:2020-12-14 01:12:57 所属栏目:Linux 来源:网络整理
导读:我有2个文本文件. File1有大约1,000行,File2有20,000行. File1的摘录如下: ThrustAlien Breed Special Edition '92amidarmariomspacmanBubble Bobble (Japan) File2的摘录如下: 005;005;Arcade-Vertical;;;;;;;;;;;;;;Alien Breed Special Edition '92;Ali
我有2个文本文件. File1有大约1,000行,File2有20,000行.
File1的摘录如下: Thrust Alien Breed Special Edition '92 amidar mario mspacman Bubble Bobble (Japan) 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;;;;;;;;;;;;;; 在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;;;;;;;;;;;;;; mario;mario;FBA;;;;;;;;;;;;;; mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;; Bubble Bobble (Japan);Bubble Bobble (Japan);msx2;;;;;;;;;;;;;; 例如,当我使用Grep生成File3时,我发现它会自动对文件内容进行排序.我想保持与File1相同的顺序. 我用过的代码的一个例子最终排序File3(我不想要)如下: grep -F -w -f /home/pi/.attract/stats/File1.txt /home/pi/.attract/stats/File2.txt > /home/pi/.attract/stats/File3.txt 解决方法
使用awk:
$awk -F; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}' file2 file1 输出: Thrust;Thrust;BBC Micro;;;;;;;;;;;;;; Alien Breed Special Edition '92;Alien Breed Special Edition '92;Amiga;;1992;Team 17;Action / Shooter;;;;;;;;;; amidar;amidar;Arcade-Vertical;;;;;;;;;;;;;; mario;mario;FBA;;;;;;;;;;;;;; mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;; Bubble Bobble (Japan);Bubble Bobble (Japan);msx2;;;;;;;;;;;;;; 解释: awk -F; ' NR==FNR { # process file2 a[$1]=$0 # hash record to a,use $1 as key next # process next record } ($1 in a) { # if file1 entry is found in hash a print a[$1] # output it }' file2 file1 # mind the order. this way file1 dictates the output order (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |