linux: vlookup using one file to another

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












-1















I want to print matching strings in File1 using fields of File2 (standard file). I have tried below awk command and but only first line results are being printed, instead I would like to get all fields matching from File1 strings.



 cat File1
AAA,20190221,00-00,100
AAA,20190221,01-00,110
AAA,20190221,02-00,120
BBB,20190221,00-00,110
BBB,20190221,01-00,110
BBB,20190221,02-00,110
CCC,20190221,00-00,110
CCC,20190221,01-00,140
CCC,20190221,02-00,150

cat File2
AAA
BBB


I am using below awk method,



 awk -F"," -f vlookup.awk File1 File2
(cat vlookup.awk)
FNR==NR
a[$1]=$4
next
if ($1 in a) print $1"," a[$1] else print $1, "NA"


I am getting output as



 AAA,100
BBB,110
NA


but I need to print all the values from File1, that may increase or decrease time to time.
my expected output is



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110









share|improve this question
























  • Please show the expected output for your example.

    – Bodo
    Feb 22 at 13:01











  • my expected output is AAA,100 AAA,110 AAA,120 BBB,110 BBB,110 BBB,110

    – SSK
    Feb 22 at 13:02
















-1















I want to print matching strings in File1 using fields of File2 (standard file). I have tried below awk command and but only first line results are being printed, instead I would like to get all fields matching from File1 strings.



 cat File1
AAA,20190221,00-00,100
AAA,20190221,01-00,110
AAA,20190221,02-00,120
BBB,20190221,00-00,110
BBB,20190221,01-00,110
BBB,20190221,02-00,110
CCC,20190221,00-00,110
CCC,20190221,01-00,140
CCC,20190221,02-00,150

cat File2
AAA
BBB


I am using below awk method,



 awk -F"," -f vlookup.awk File1 File2
(cat vlookup.awk)
FNR==NR
a[$1]=$4
next
if ($1 in a) print $1"," a[$1] else print $1, "NA"


I am getting output as



 AAA,100
BBB,110
NA


but I need to print all the values from File1, that may increase or decrease time to time.
my expected output is



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110









share|improve this question
























  • Please show the expected output for your example.

    – Bodo
    Feb 22 at 13:01











  • my expected output is AAA,100 AAA,110 AAA,120 BBB,110 BBB,110 BBB,110

    – SSK
    Feb 22 at 13:02














-1












-1








-1








I want to print matching strings in File1 using fields of File2 (standard file). I have tried below awk command and but only first line results are being printed, instead I would like to get all fields matching from File1 strings.



 cat File1
AAA,20190221,00-00,100
AAA,20190221,01-00,110
AAA,20190221,02-00,120
BBB,20190221,00-00,110
BBB,20190221,01-00,110
BBB,20190221,02-00,110
CCC,20190221,00-00,110
CCC,20190221,01-00,140
CCC,20190221,02-00,150

cat File2
AAA
BBB


I am using below awk method,



 awk -F"," -f vlookup.awk File1 File2
(cat vlookup.awk)
FNR==NR
a[$1]=$4
next
if ($1 in a) print $1"," a[$1] else print $1, "NA"


I am getting output as



 AAA,100
BBB,110
NA


but I need to print all the values from File1, that may increase or decrease time to time.
my expected output is



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110









share|improve this question
















I want to print matching strings in File1 using fields of File2 (standard file). I have tried below awk command and but only first line results are being printed, instead I would like to get all fields matching from File1 strings.



 cat File1
AAA,20190221,00-00,100
AAA,20190221,01-00,110
AAA,20190221,02-00,120
BBB,20190221,00-00,110
BBB,20190221,01-00,110
BBB,20190221,02-00,110
CCC,20190221,00-00,110
CCC,20190221,01-00,140
CCC,20190221,02-00,150

cat File2
AAA
BBB


I am using below awk method,



 awk -F"," -f vlookup.awk File1 File2
(cat vlookup.awk)
FNR==NR
a[$1]=$4
next
if ($1 in a) print $1"," a[$1] else print $1, "NA"


I am getting output as



 AAA,100
BBB,110
NA


but I need to print all the values from File1, that may increase or decrease time to time.
my expected output is



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110






linux shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 22 at 13:04







SSK

















asked Feb 22 at 12:58









SSKSSK

82




82












  • Please show the expected output for your example.

    – Bodo
    Feb 22 at 13:01











  • my expected output is AAA,100 AAA,110 AAA,120 BBB,110 BBB,110 BBB,110

    – SSK
    Feb 22 at 13:02


















  • Please show the expected output for your example.

    – Bodo
    Feb 22 at 13:01











  • my expected output is AAA,100 AAA,110 AAA,120 BBB,110 BBB,110 BBB,110

    – SSK
    Feb 22 at 13:02

















Please show the expected output for your example.

– Bodo
Feb 22 at 13:01





Please show the expected output for your example.

– Bodo
Feb 22 at 13:01













my expected output is AAA,100 AAA,110 AAA,120 BBB,110 BBB,110 BBB,110

– SSK
Feb 22 at 13:02






my expected output is AAA,100 AAA,110 AAA,120 BBB,110 BBB,110 BBB,110

– SSK
Feb 22 at 13:02











1 Answer
1






active

oldest

votes


















1














Your AWK script saves from File1 the last value from column 4 for every key from column 1 into array a using column 1 as index. Then it prints all array keys and values that are present in File2.



You should change the order of the input files and create an array of keys from File2, then compare the lines from File1 with this array.



vlookup2.awk



FNR==NR 
a[$0]=1 # Set a value in array a using current line as index
next # Stop processing this line, do not check other rules

$1 in a # if column1 matches any index in array a
print $1 "," $4 # the columns you want



Run



awk -F "," -f vlookup2.awk File2 File1


to get



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110


Note: I used File2 as the first argument.



BTW: There seems to be something wrong with your AWK script, your sample files and the output. I get two lines



AAA,120
BBB,110


which are the last lines from File1 with the corresponding key in column 1. Your example output shows the first line for AAA.



I get a line



 NA


(with a space before NA) only if File2 contains an empty line after BBB.






share|improve this answer























  • works perfect.. thanks a lot.

    – SSK
    Feb 28 at 7:09










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502289%2flinux-vlookup-using-one-file-to-another%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









1














Your AWK script saves from File1 the last value from column 4 for every key from column 1 into array a using column 1 as index. Then it prints all array keys and values that are present in File2.



You should change the order of the input files and create an array of keys from File2, then compare the lines from File1 with this array.



vlookup2.awk



FNR==NR 
a[$0]=1 # Set a value in array a using current line as index
next # Stop processing this line, do not check other rules

$1 in a # if column1 matches any index in array a
print $1 "," $4 # the columns you want



Run



awk -F "," -f vlookup2.awk File2 File1


to get



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110


Note: I used File2 as the first argument.



BTW: There seems to be something wrong with your AWK script, your sample files and the output. I get two lines



AAA,120
BBB,110


which are the last lines from File1 with the corresponding key in column 1. Your example output shows the first line for AAA.



I get a line



 NA


(with a space before NA) only if File2 contains an empty line after BBB.






share|improve this answer























  • works perfect.. thanks a lot.

    – SSK
    Feb 28 at 7:09















1














Your AWK script saves from File1 the last value from column 4 for every key from column 1 into array a using column 1 as index. Then it prints all array keys and values that are present in File2.



You should change the order of the input files and create an array of keys from File2, then compare the lines from File1 with this array.



vlookup2.awk



FNR==NR 
a[$0]=1 # Set a value in array a using current line as index
next # Stop processing this line, do not check other rules

$1 in a # if column1 matches any index in array a
print $1 "," $4 # the columns you want



Run



awk -F "," -f vlookup2.awk File2 File1


to get



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110


Note: I used File2 as the first argument.



BTW: There seems to be something wrong with your AWK script, your sample files and the output. I get two lines



AAA,120
BBB,110


which are the last lines from File1 with the corresponding key in column 1. Your example output shows the first line for AAA.



I get a line



 NA


(with a space before NA) only if File2 contains an empty line after BBB.






share|improve this answer























  • works perfect.. thanks a lot.

    – SSK
    Feb 28 at 7:09













1












1








1







Your AWK script saves from File1 the last value from column 4 for every key from column 1 into array a using column 1 as index. Then it prints all array keys and values that are present in File2.



You should change the order of the input files and create an array of keys from File2, then compare the lines from File1 with this array.



vlookup2.awk



FNR==NR 
a[$0]=1 # Set a value in array a using current line as index
next # Stop processing this line, do not check other rules

$1 in a # if column1 matches any index in array a
print $1 "," $4 # the columns you want



Run



awk -F "," -f vlookup2.awk File2 File1


to get



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110


Note: I used File2 as the first argument.



BTW: There seems to be something wrong with your AWK script, your sample files and the output. I get two lines



AAA,120
BBB,110


which are the last lines from File1 with the corresponding key in column 1. Your example output shows the first line for AAA.



I get a line



 NA


(with a space before NA) only if File2 contains an empty line after BBB.






share|improve this answer













Your AWK script saves from File1 the last value from column 4 for every key from column 1 into array a using column 1 as index. Then it prints all array keys and values that are present in File2.



You should change the order of the input files and create an array of keys from File2, then compare the lines from File1 with this array.



vlookup2.awk



FNR==NR 
a[$0]=1 # Set a value in array a using current line as index
next # Stop processing this line, do not check other rules

$1 in a # if column1 matches any index in array a
print $1 "," $4 # the columns you want



Run



awk -F "," -f vlookup2.awk File2 File1


to get



AAA,100
AAA,110
AAA,120
BBB,110
BBB,110
BBB,110


Note: I used File2 as the first argument.



BTW: There seems to be something wrong with your AWK script, your sample files and the output. I get two lines



AAA,120
BBB,110


which are the last lines from File1 with the corresponding key in column 1. Your example output shows the first line for AAA.



I get a line



 NA


(with a space before NA) only if File2 contains an empty line after BBB.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 22 at 14:39









BodoBodo

2,251618




2,251618












  • works perfect.. thanks a lot.

    – SSK
    Feb 28 at 7:09

















  • works perfect.. thanks a lot.

    – SSK
    Feb 28 at 7:09
















works perfect.. thanks a lot.

– SSK
Feb 28 at 7:09





works perfect.. thanks a lot.

– SSK
Feb 28 at 7:09

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502289%2flinux-vlookup-using-one-file-to-another%23new-answer', 'question_page');

);

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






Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)