Linux   发布时间:2022-05-08  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了是否可以使用Grep,Sed或Awk或bash脚本而不对输出文件进行排序?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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

大佬总结

以上是大佬教程为你收集整理的是否可以使用Grep,Sed或Awk或bash脚本而不对输出文件进行排序?全部内容,希望文章能够帮你解决是否可以使用Grep,Sed或Awk或bash脚本而不对输出文件进行排序?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:000行