5
votes

I have 2 text files. File1 has about 1,000 lines and File2 has 20,000 lines. An extract of File1 is as follows:

Thrust
Alien Breed Special Edition '92
amidar
mario
mspacman
Bubble Bobble (Japan)

An extract of File2 is as follows:

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;;;;;;;;;;;;;;

In File3 (the output file), using grep, sed, awk or a bash script, I would like to achieve the following output:

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;;;;;;;;;;;;;;

When I used Grep, for example, to produce File3, I found that it automatically sorts the file contents. I would like to keep the same order as for File1.

An example of code I have used which winds up sorting File3 (which I don't want) is as follows:

grep -F -w -f /home/pi/.attract/stats/File1.txt /home/pi/.attract/stats/File2.txt > /home/pi/.attract/stats/File3.txt
1
Just to be clear, your output is not sorted, grep gives the same order as is given in file2, which is accidentally ordered case-insensitively.kvantour
Thanks. I picked that up from James Brown's answer. I hadn't realised that, so it is very useful to know.Spud

1 Answers

7
votes

Using awk:

$ awk -F\; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}' file2 file1

Output:

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;;;;;;;;;;;;;;

Explained:

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