How to do awk to do sorting between a txt file and a csv file?

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











up vote
-2
down vote

favorite












Here is the file that I am going to use:



file1.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544


file2.csv:



Date,JUPAS,UniType,DSE,PRA ,PE,SIP,BRA,DDI Group,IDAM,"Working line",,
14-Oct-16,6,HKU,MAT SS2,278,806,248,0,0,0,32907,,
14-Oct-16,8,HKU,CHN SS4,231,726,250,0,0,0,23953,,
14-Oct-16,10,HKU,ENG SS6,593,3,11,0,0,0,4511,,
14-Oct-16,12,HKU,PHY SS8,664,16,12,0,0,0,3946,,
14-Oct-16,7,HKU,BIO SS3,297,225,11,0,0,0,18281,,
14-Oct-16,9,CU,CHE SS1,371,145,23,191,0,0,40570,,
14-Oct-16,11,CU,BIO SS3,336,90,0,100,0,0,36141,,
14-Oct-16,13,CU,CHE SS1,358,116,19,276,0,0,41559,,
,Total,,,7581 ,265926 ,5404 ,1150 ,1347 ,309 ,1136379 ,,
,,,,,,,,,,,,
,,,,,,,,,,,,
Date,Vendor,DSE,Working line,,,,,,,,,
14-Oct-16,SKH,MAT SS,269339,,,,,,,,,
14-Oct-16,SKH,CHN SS,270038,,,,,,,,,
14-Oct-16,SKH,ENG SS,144931,,,,,,,,,
14-Oct-16,LTH,BIO SS,154598,,,,,,,,,
14-Oct-16,LTH,CHE SS (BRA),817,,,,,,,,,
14-Oct-16,LTH,BIO SS (BRA),333,,,,,,,,,


How can I sort the data from file2.csv that JUPAS column matched with JUPAS_N column of file1.txt and output the following result text file:



result.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544 36141


Here is what I have tried before:



awk -F "," 'NR==FNRa[$2]=$11;next print $0, a[$2]' file2.csv FS='[ -]' file1.txt > result.txt


After using the above code, what I got is a text file with the same content with file1.txt . What code should I change in order to get the expected result above? Or should I use the sed function to do that?









share





















  • Hello, @Owen the results in your question are not clear hw generated would you please elaborate more?
    – Goro
    6 mins ago










  • JUPAS_N in file 1 is 11-2 and JUPAS in file 2 is 11 ? it is correct?
    – Ø¨Ø§Ø±Ù¾Ø§Ø¨Ø§Ø¨Ø§
    5 mins ago














up vote
-2
down vote

favorite












Here is the file that I am going to use:



file1.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544


file2.csv:



Date,JUPAS,UniType,DSE,PRA ,PE,SIP,BRA,DDI Group,IDAM,"Working line",,
14-Oct-16,6,HKU,MAT SS2,278,806,248,0,0,0,32907,,
14-Oct-16,8,HKU,CHN SS4,231,726,250,0,0,0,23953,,
14-Oct-16,10,HKU,ENG SS6,593,3,11,0,0,0,4511,,
14-Oct-16,12,HKU,PHY SS8,664,16,12,0,0,0,3946,,
14-Oct-16,7,HKU,BIO SS3,297,225,11,0,0,0,18281,,
14-Oct-16,9,CU,CHE SS1,371,145,23,191,0,0,40570,,
14-Oct-16,11,CU,BIO SS3,336,90,0,100,0,0,36141,,
14-Oct-16,13,CU,CHE SS1,358,116,19,276,0,0,41559,,
,Total,,,7581 ,265926 ,5404 ,1150 ,1347 ,309 ,1136379 ,,
,,,,,,,,,,,,
,,,,,,,,,,,,
Date,Vendor,DSE,Working line,,,,,,,,,
14-Oct-16,SKH,MAT SS,269339,,,,,,,,,
14-Oct-16,SKH,CHN SS,270038,,,,,,,,,
14-Oct-16,SKH,ENG SS,144931,,,,,,,,,
14-Oct-16,LTH,BIO SS,154598,,,,,,,,,
14-Oct-16,LTH,CHE SS (BRA),817,,,,,,,,,
14-Oct-16,LTH,BIO SS (BRA),333,,,,,,,,,


How can I sort the data from file2.csv that JUPAS column matched with JUPAS_N column of file1.txt and output the following result text file:



result.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544 36141


Here is what I have tried before:



awk -F "," 'NR==FNRa[$2]=$11;next print $0, a[$2]' file2.csv FS='[ -]' file1.txt > result.txt


After using the above code, what I got is a text file with the same content with file1.txt . What code should I change in order to get the expected result above? Or should I use the sed function to do that?









share





















  • Hello, @Owen the results in your question are not clear hw generated would you please elaborate more?
    – Goro
    6 mins ago










  • JUPAS_N in file 1 is 11-2 and JUPAS in file 2 is 11 ? it is correct?
    – Ø¨Ø§Ø±Ù¾Ø§Ø¨Ø§Ø¨Ø§
    5 mins ago












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Here is the file that I am going to use:



file1.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544


file2.csv:



Date,JUPAS,UniType,DSE,PRA ,PE,SIP,BRA,DDI Group,IDAM,"Working line",,
14-Oct-16,6,HKU,MAT SS2,278,806,248,0,0,0,32907,,
14-Oct-16,8,HKU,CHN SS4,231,726,250,0,0,0,23953,,
14-Oct-16,10,HKU,ENG SS6,593,3,11,0,0,0,4511,,
14-Oct-16,12,HKU,PHY SS8,664,16,12,0,0,0,3946,,
14-Oct-16,7,HKU,BIO SS3,297,225,11,0,0,0,18281,,
14-Oct-16,9,CU,CHE SS1,371,145,23,191,0,0,40570,,
14-Oct-16,11,CU,BIO SS3,336,90,0,100,0,0,36141,,
14-Oct-16,13,CU,CHE SS1,358,116,19,276,0,0,41559,,
,Total,,,7581 ,265926 ,5404 ,1150 ,1347 ,309 ,1136379 ,,
,,,,,,,,,,,,
,,,,,,,,,,,,
Date,Vendor,DSE,Working line,,,,,,,,,
14-Oct-16,SKH,MAT SS,269339,,,,,,,,,
14-Oct-16,SKH,CHN SS,270038,,,,,,,,,
14-Oct-16,SKH,ENG SS,144931,,,,,,,,,
14-Oct-16,LTH,BIO SS,154598,,,,,,,,,
14-Oct-16,LTH,CHE SS (BRA),817,,,,,,,,,
14-Oct-16,LTH,BIO SS (BRA),333,,,,,,,,,


How can I sort the data from file2.csv that JUPAS column matched with JUPAS_N column of file1.txt and output the following result text file:



result.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544 36141


Here is what I have tried before:



awk -F "," 'NR==FNRa[$2]=$11;next print $0, a[$2]' file2.csv FS='[ -]' file1.txt > result.txt


After using the above code, what I got is a text file with the same content with file1.txt . What code should I change in order to get the expected result above? Or should I use the sed function to do that?









share













Here is the file that I am going to use:



file1.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544


file2.csv:



Date,JUPAS,UniType,DSE,PRA ,PE,SIP,BRA,DDI Group,IDAM,"Working line",,
14-Oct-16,6,HKU,MAT SS2,278,806,248,0,0,0,32907,,
14-Oct-16,8,HKU,CHN SS4,231,726,250,0,0,0,23953,,
14-Oct-16,10,HKU,ENG SS6,593,3,11,0,0,0,4511,,
14-Oct-16,12,HKU,PHY SS8,664,16,12,0,0,0,3946,,
14-Oct-16,7,HKU,BIO SS3,297,225,11,0,0,0,18281,,
14-Oct-16,9,CU,CHE SS1,371,145,23,191,0,0,40570,,
14-Oct-16,11,CU,BIO SS3,336,90,0,100,0,0,36141,,
14-Oct-16,13,CU,CHE SS1,358,116,19,276,0,0,41559,,
,Total,,,7581 ,265926 ,5404 ,1150 ,1347 ,309 ,1136379 ,,
,,,,,,,,,,,,
,,,,,,,,,,,,
Date,Vendor,DSE,Working line,,,,,,,,,
14-Oct-16,SKH,MAT SS,269339,,,,,,,,,
14-Oct-16,SKH,CHN SS,270038,,,,,,,,,
14-Oct-16,SKH,ENG SS,144931,,,,,,,,,
14-Oct-16,LTH,BIO SS,154598,,,,,,,,,
14-Oct-16,LTH,CHE SS (BRA),817,,,,,,,,,
14-Oct-16,LTH,BIO SS (BRA),333,,,,,,,,,


How can I sort the data from file2.csv that JUPAS column matched with JUPAS_N column of file1.txt and output the following result text file:



result.txt:



School JUPAS_N Univ ID Working_line_number
TST 11-2 CUHK 90544 36141


Here is what I have tried before:



awk -F "," 'NR==FNRa[$2]=$11;next print $0, a[$2]' file2.csv FS='[ -]' file1.txt > result.txt


After using the above code, what I got is a text file with the same content with file1.txt . What code should I change in order to get the expected result above? Or should I use the sed function to do that?







linux bash shell-script awk csv





share












share










share



share










asked 9 mins ago









Owen

92




92











  • Hello, @Owen the results in your question are not clear hw generated would you please elaborate more?
    – Goro
    6 mins ago










  • JUPAS_N in file 1 is 11-2 and JUPAS in file 2 is 11 ? it is correct?
    – Ø¨Ø§Ø±Ù¾Ø§Ø¨Ø§Ø¨Ø§
    5 mins ago
















  • Hello, @Owen the results in your question are not clear hw generated would you please elaborate more?
    – Goro
    6 mins ago










  • JUPAS_N in file 1 is 11-2 and JUPAS in file 2 is 11 ? it is correct?
    – Ø¨Ø§Ø±Ù¾Ø§Ø¨Ø§Ø¨Ø§
    5 mins ago















Hello, @Owen the results in your question are not clear hw generated would you please elaborate more?
– Goro
6 mins ago




Hello, @Owen the results in your question are not clear hw generated would you please elaborate more?
– Goro
6 mins ago












JUPAS_N in file 1 is 11-2 and JUPAS in file 2 is 11 ? it is correct?
– Ø¨Ø§Ø±Ù¾Ø§Ø¨Ø§Ø¨Ø§
5 mins ago




JUPAS_N in file 1 is 11-2 and JUPAS in file 2 is 11 ? it is correct?
– Ø¨Ø§Ø±Ù¾Ø§Ø¨Ø§Ø¨Ø§
5 mins ago















active

oldest

votes











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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
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%2f474947%2fhow-to-do-awk-to-do-sorting-between-a-txt-file-and-a-csv-file%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474947%2fhow-to-do-awk-to-do-sorting-between-a-txt-file-and-a-csv-file%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?