Is it possible to take 2 columns from File 1, find them in File 2 and extract the relevant lines from File 2...
I have 2 text files. File1 has about 1,000 lines and File2 has 20,000 lines. An extract of File1 is as follows:
/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
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;;;;;;;;;;;;;;
Thrust;Thrust;Vectrex;;;;;;;;;;;;;;
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
This is similar to a previous question I asked but not the same. I specifically want to avoid the possibility of Thrust;Thrust;Vectrex;;;;;;;;;;;;;; being recorded in File 3.
Using sudo awk -F; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}', I found that Thrust;Thrust;Vectrex;;;;;;;;;;;;;; was recorded in File 3 instead of Thrust;Thrust;BBC Micro;;;;;;;;;;;;;; (the latter being the output I'm seeking).
Equally, mario;mario;FBA;;;;;;;;;;;;;; won't appear in File3 because it does not match /MAME (Advance)/mario as "MAME (Advance)" doesn't match. That is good. The same for Bubble Bobble (Japan);Bubble Bobble (Japan);msx2;;;;;;;;;;;;;; which doesn't match either "Sharp X68000" or "Bubble Bobble (1989)(Dempa)".
bash awk sed grep
|
show 1 more comment
I have 2 text files. File1 has about 1,000 lines and File2 has 20,000 lines. An extract of File1 is as follows:
/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
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;;;;;;;;;;;;;;
Thrust;Thrust;Vectrex;;;;;;;;;;;;;;
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
This is similar to a previous question I asked but not the same. I specifically want to avoid the possibility of Thrust;Thrust;Vectrex;;;;;;;;;;;;;; being recorded in File 3.
Using sudo awk -F; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}', I found that Thrust;Thrust;Vectrex;;;;;;;;;;;;;; was recorded in File 3 instead of Thrust;Thrust;BBC Micro;;;;;;;;;;;;;; (the latter being the output I'm seeking).
Equally, mario;mario;FBA;;;;;;;;;;;;;; won't appear in File3 because it does not match /MAME (Advance)/mario as "MAME (Advance)" doesn't match. That is good. The same for Bubble Bobble (Japan);Bubble Bobble (Japan);msx2;;;;;;;;;;;;;; which doesn't match either "Sharp X68000" or "Bubble Bobble (1989)(Dempa)".
bash awk sed grep
The criteria for selecting lines are not at all clear. Using;
as the FS, $1 of file1 is the whole line. Please spell out in detail how to select lines from file2 based on contents of file1
– glenn jackman
Nov 20 at 23:18
For example, if both the 1st and 2nd columns of File 1 (eg BBC Micro and Thrust) are found on a single line in File 2 (eg Thrust;Thrust;BBC Micro;;;;;;;;;), then that line (Thrust;Thrust;BBC Micro;;;;;;;) will be recorded in File 3. The line in File 2, being Thrust;Thrust;Vectrex;;;;;;;; will not be recorded in File 3 because it does not match BBC Micro.
– Spud
Nov 20 at 23:21
You were correct. Sorry about that. I needed to change File3 to reflect the correct output.
– Spud
Nov 21 at 1:06
It appears that you can match "column 2" in file1 with the 1st field in file2, and "column 1" in file2 with the 3rd field in file2. Is that your rules for matching? Or, as you say, the data from file2 just has to "match"?
– glenn jackman
Nov 21 at 3:11
Thanks, Glenn. That is a correct summary of what I'm looking for. Sorry about the question not being clear.
– Spud
Nov 21 at 3:49
|
show 1 more comment
I have 2 text files. File1 has about 1,000 lines and File2 has 20,000 lines. An extract of File1 is as follows:
/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
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;;;;;;;;;;;;;;
Thrust;Thrust;Vectrex;;;;;;;;;;;;;;
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
This is similar to a previous question I asked but not the same. I specifically want to avoid the possibility of Thrust;Thrust;Vectrex;;;;;;;;;;;;;; being recorded in File 3.
Using sudo awk -F; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}', I found that Thrust;Thrust;Vectrex;;;;;;;;;;;;;; was recorded in File 3 instead of Thrust;Thrust;BBC Micro;;;;;;;;;;;;;; (the latter being the output I'm seeking).
Equally, mario;mario;FBA;;;;;;;;;;;;;; won't appear in File3 because it does not match /MAME (Advance)/mario as "MAME (Advance)" doesn't match. That is good. The same for Bubble Bobble (Japan);Bubble Bobble (Japan);msx2;;;;;;;;;;;;;; which doesn't match either "Sharp X68000" or "Bubble Bobble (1989)(Dempa)".
bash awk sed grep
I have 2 text files. File1 has about 1,000 lines and File2 has 20,000 lines. An extract of File1 is as follows:
/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
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;;;;;;;;;;;;;;
Thrust;Thrust;Vectrex;;;;;;;;;;;;;;
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
This is similar to a previous question I asked but not the same. I specifically want to avoid the possibility of Thrust;Thrust;Vectrex;;;;;;;;;;;;;; being recorded in File 3.
Using sudo awk -F; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}', I found that Thrust;Thrust;Vectrex;;;;;;;;;;;;;; was recorded in File 3 instead of Thrust;Thrust;BBC Micro;;;;;;;;;;;;;; (the latter being the output I'm seeking).
Equally, mario;mario;FBA;;;;;;;;;;;;;; won't appear in File3 because it does not match /MAME (Advance)/mario as "MAME (Advance)" doesn't match. That is good. The same for Bubble Bobble (Japan);Bubble Bobble (Japan);msx2;;;;;;;;;;;;;; which doesn't match either "Sharp X68000" or "Bubble Bobble (1989)(Dempa)".
bash awk sed grep
bash awk sed grep
edited Nov 21 at 1:29
asked Nov 20 at 23:04
Spud
354
354
The criteria for selecting lines are not at all clear. Using;
as the FS, $1 of file1 is the whole line. Please spell out in detail how to select lines from file2 based on contents of file1
– glenn jackman
Nov 20 at 23:18
For example, if both the 1st and 2nd columns of File 1 (eg BBC Micro and Thrust) are found on a single line in File 2 (eg Thrust;Thrust;BBC Micro;;;;;;;;;), then that line (Thrust;Thrust;BBC Micro;;;;;;;) will be recorded in File 3. The line in File 2, being Thrust;Thrust;Vectrex;;;;;;;; will not be recorded in File 3 because it does not match BBC Micro.
– Spud
Nov 20 at 23:21
You were correct. Sorry about that. I needed to change File3 to reflect the correct output.
– Spud
Nov 21 at 1:06
It appears that you can match "column 2" in file1 with the 1st field in file2, and "column 1" in file2 with the 3rd field in file2. Is that your rules for matching? Or, as you say, the data from file2 just has to "match"?
– glenn jackman
Nov 21 at 3:11
Thanks, Glenn. That is a correct summary of what I'm looking for. Sorry about the question not being clear.
– Spud
Nov 21 at 3:49
|
show 1 more comment
The criteria for selecting lines are not at all clear. Using;
as the FS, $1 of file1 is the whole line. Please spell out in detail how to select lines from file2 based on contents of file1
– glenn jackman
Nov 20 at 23:18
For example, if both the 1st and 2nd columns of File 1 (eg BBC Micro and Thrust) are found on a single line in File 2 (eg Thrust;Thrust;BBC Micro;;;;;;;;;), then that line (Thrust;Thrust;BBC Micro;;;;;;;) will be recorded in File 3. The line in File 2, being Thrust;Thrust;Vectrex;;;;;;;; will not be recorded in File 3 because it does not match BBC Micro.
– Spud
Nov 20 at 23:21
You were correct. Sorry about that. I needed to change File3 to reflect the correct output.
– Spud
Nov 21 at 1:06
It appears that you can match "column 2" in file1 with the 1st field in file2, and "column 1" in file2 with the 3rd field in file2. Is that your rules for matching? Or, as you say, the data from file2 just has to "match"?
– glenn jackman
Nov 21 at 3:11
Thanks, Glenn. That is a correct summary of what I'm looking for. Sorry about the question not being clear.
– Spud
Nov 21 at 3:49
The criteria for selecting lines are not at all clear. Using
;
as the FS, $1 of file1 is the whole line. Please spell out in detail how to select lines from file2 based on contents of file1– glenn jackman
Nov 20 at 23:18
The criteria for selecting lines are not at all clear. Using
;
as the FS, $1 of file1 is the whole line. Please spell out in detail how to select lines from file2 based on contents of file1– glenn jackman
Nov 20 at 23:18
For example, if both the 1st and 2nd columns of File 1 (eg BBC Micro and Thrust) are found on a single line in File 2 (eg Thrust;Thrust;BBC Micro;;;;;;;;;), then that line (Thrust;Thrust;BBC Micro;;;;;;;) will be recorded in File 3. The line in File 2, being Thrust;Thrust;Vectrex;;;;;;;; will not be recorded in File 3 because it does not match BBC Micro.
– Spud
Nov 20 at 23:21
For example, if both the 1st and 2nd columns of File 1 (eg BBC Micro and Thrust) are found on a single line in File 2 (eg Thrust;Thrust;BBC Micro;;;;;;;;;), then that line (Thrust;Thrust;BBC Micro;;;;;;;) will be recorded in File 3. The line in File 2, being Thrust;Thrust;Vectrex;;;;;;;; will not be recorded in File 3 because it does not match BBC Micro.
– Spud
Nov 20 at 23:21
You were correct. Sorry about that. I needed to change File3 to reflect the correct output.
– Spud
Nov 21 at 1:06
You were correct. Sorry about that. I needed to change File3 to reflect the correct output.
– Spud
Nov 21 at 1:06
It appears that you can match "column 2" in file1 with the 1st field in file2, and "column 1" in file2 with the 3rd field in file2. Is that your rules for matching? Or, as you say, the data from file2 just has to "match"?
– glenn jackman
Nov 21 at 3:11
It appears that you can match "column 2" in file1 with the 1st field in file2, and "column 1" in file2 with the 3rd field in file2. Is that your rules for matching? Or, as you say, the data from file2 just has to "match"?
– glenn jackman
Nov 21 at 3:11
Thanks, Glenn. That is a correct summary of what I'm looking for. Sorry about the question not being clear.
– Spud
Nov 21 at 3:49
Thanks, Glenn. That is a correct summary of what I'm looking for. Sorry about the question not being clear.
– Spud
Nov 21 at 3:49
|
show 1 more comment
1 Answer
1
active
oldest
votes
Using AWK and associative array You can use this:
awk '
BEGIN {
if ( ARGC != 3 ) exit(1);
FS="/";
while ( getline < ARGV[2] ) mfggames[$2"/"$3]=1;
FS=";";
ARGC=2;
}
mfggames[$3"/"$1]
' file2 file1
Output:
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;;;;;;;;;;;;;;
Sorted per file1 solution (as per comment request):
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-
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
Thanks, Kubator. But I need the order to be in the same order as I have it - ie same order as File1/File3 - not sorted as in your Output.
– Spud
Nov 21 at 11:45
Basically, File1 needs to dictate the file order.
– Spud
Nov 21 at 12:47
OK, added as "Sorted per file1 solution" in my answer. Unfortunately this wan't emphasized in original question.
– Kubator
Nov 21 at 15:17
Thank you. That does appear to work for me. Could I ask please whether it is possible to have this code on one line? If not, that is fine. I will upvote your solution. Thank you again.
– Spud
Nov 21 at 21:53
One line? Yes just remove NLs: 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-
– Kubator
Nov 22 at 8:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402938%2fis-it-possible-to-take-2-columns-from-file-1-find-them-in-file-2-and-extract-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using AWK and associative array You can use this:
awk '
BEGIN {
if ( ARGC != 3 ) exit(1);
FS="/";
while ( getline < ARGV[2] ) mfggames[$2"/"$3]=1;
FS=";";
ARGC=2;
}
mfggames[$3"/"$1]
' file2 file1
Output:
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;;;;;;;;;;;;;;
Sorted per file1 solution (as per comment request):
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-
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
Thanks, Kubator. But I need the order to be in the same order as I have it - ie same order as File1/File3 - not sorted as in your Output.
– Spud
Nov 21 at 11:45
Basically, File1 needs to dictate the file order.
– Spud
Nov 21 at 12:47
OK, added as "Sorted per file1 solution" in my answer. Unfortunately this wan't emphasized in original question.
– Kubator
Nov 21 at 15:17
Thank you. That does appear to work for me. Could I ask please whether it is possible to have this code on one line? If not, that is fine. I will upvote your solution. Thank you again.
– Spud
Nov 21 at 21:53
One line? Yes just remove NLs: 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-
– Kubator
Nov 22 at 8:29
add a comment |
Using AWK and associative array You can use this:
awk '
BEGIN {
if ( ARGC != 3 ) exit(1);
FS="/";
while ( getline < ARGV[2] ) mfggames[$2"/"$3]=1;
FS=";";
ARGC=2;
}
mfggames[$3"/"$1]
' file2 file1
Output:
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;;;;;;;;;;;;;;
Sorted per file1 solution (as per comment request):
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-
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
Thanks, Kubator. But I need the order to be in the same order as I have it - ie same order as File1/File3 - not sorted as in your Output.
– Spud
Nov 21 at 11:45
Basically, File1 needs to dictate the file order.
– Spud
Nov 21 at 12:47
OK, added as "Sorted per file1 solution" in my answer. Unfortunately this wan't emphasized in original question.
– Kubator
Nov 21 at 15:17
Thank you. That does appear to work for me. Could I ask please whether it is possible to have this code on one line? If not, that is fine. I will upvote your solution. Thank you again.
– Spud
Nov 21 at 21:53
One line? Yes just remove NLs: 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-
– Kubator
Nov 22 at 8:29
add a comment |
Using AWK and associative array You can use this:
awk '
BEGIN {
if ( ARGC != 3 ) exit(1);
FS="/";
while ( getline < ARGV[2] ) mfggames[$2"/"$3]=1;
FS=";";
ARGC=2;
}
mfggames[$3"/"$1]
' file2 file1
Output:
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;;;;;;;;;;;;;;
Sorted per file1 solution (as per comment request):
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-
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
Using AWK and associative array You can use this:
awk '
BEGIN {
if ( ARGC != 3 ) exit(1);
FS="/";
while ( getline < ARGV[2] ) mfggames[$2"/"$3]=1;
FS=";";
ARGC=2;
}
mfggames[$3"/"$1]
' file2 file1
Output:
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;;;;;;;;;;;;;;
Sorted per file1 solution (as per comment request):
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-
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;;;;;;;;;;;;;;
mspacman;mspacman;Arcade-Vertical;;;;;;;;;;;;;;
edited Nov 21 at 15:15
answered Nov 21 at 10:22
Kubator
69911
69911
Thanks, Kubator. But I need the order to be in the same order as I have it - ie same order as File1/File3 - not sorted as in your Output.
– Spud
Nov 21 at 11:45
Basically, File1 needs to dictate the file order.
– Spud
Nov 21 at 12:47
OK, added as "Sorted per file1 solution" in my answer. Unfortunately this wan't emphasized in original question.
– Kubator
Nov 21 at 15:17
Thank you. That does appear to work for me. Could I ask please whether it is possible to have this code on one line? If not, that is fine. I will upvote your solution. Thank you again.
– Spud
Nov 21 at 21:53
One line? Yes just remove NLs: 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-
– Kubator
Nov 22 at 8:29
add a comment |
Thanks, Kubator. But I need the order to be in the same order as I have it - ie same order as File1/File3 - not sorted as in your Output.
– Spud
Nov 21 at 11:45
Basically, File1 needs to dictate the file order.
– Spud
Nov 21 at 12:47
OK, added as "Sorted per file1 solution" in my answer. Unfortunately this wan't emphasized in original question.
– Kubator
Nov 21 at 15:17
Thank you. That does appear to work for me. Could I ask please whether it is possible to have this code on one line? If not, that is fine. I will upvote your solution. Thank you again.
– Spud
Nov 21 at 21:53
One line? Yes just remove NLs: 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-
– Kubator
Nov 22 at 8:29
Thanks, Kubator. But I need the order to be in the same order as I have it - ie same order as File1/File3 - not sorted as in your Output.
– Spud
Nov 21 at 11:45
Thanks, Kubator. But I need the order to be in the same order as I have it - ie same order as File1/File3 - not sorted as in your Output.
– Spud
Nov 21 at 11:45
Basically, File1 needs to dictate the file order.
– Spud
Nov 21 at 12:47
Basically, File1 needs to dictate the file order.
– Spud
Nov 21 at 12:47
OK, added as "Sorted per file1 solution" in my answer. Unfortunately this wan't emphasized in original question.
– Kubator
Nov 21 at 15:17
OK, added as "Sorted per file1 solution" in my answer. Unfortunately this wan't emphasized in original question.
– Kubator
Nov 21 at 15:17
Thank you. That does appear to work for me. Could I ask please whether it is possible to have this code on one line? If not, that is fine. I will upvote your solution. Thank you again.
– Spud
Nov 21 at 21:53
Thank you. That does appear to work for me. Could I ask please whether it is possible to have this code on one line? If not, that is fine. I will upvote your solution. Thank you again.
– Spud
Nov 21 at 21:53
One line? Yes just remove NLs: 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-
– Kubator
Nov 22 at 8:29
One line? Yes just remove NLs: 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-
– Kubator
Nov 22 at 8:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402938%2fis-it-possible-to-take-2-columns-from-file-1-find-them-in-file-2-and-extract-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The criteria for selecting lines are not at all clear. Using
;
as the FS, $1 of file1 is the whole line. Please spell out in detail how to select lines from file2 based on contents of file1– glenn jackman
Nov 20 at 23:18
For example, if both the 1st and 2nd columns of File 1 (eg BBC Micro and Thrust) are found on a single line in File 2 (eg Thrust;Thrust;BBC Micro;;;;;;;;;), then that line (Thrust;Thrust;BBC Micro;;;;;;;) will be recorded in File 3. The line in File 2, being Thrust;Thrust;Vectrex;;;;;;;; will not be recorded in File 3 because it does not match BBC Micro.
– Spud
Nov 20 at 23:21
You were correct. Sorry about that. I needed to change File3 to reflect the correct output.
– Spud
Nov 21 at 1:06
It appears that you can match "column 2" in file1 with the 1st field in file2, and "column 1" in file2 with the 3rd field in file2. Is that your rules for matching? Or, as you say, the data from file2 just has to "match"?
– glenn jackman
Nov 21 at 3:11
Thanks, Glenn. That is a correct summary of what I'm looking for. Sorry about the question not being clear.
– Spud
Nov 21 at 3:49