Kyle Gong Kyle Gong - 25 days ago 7
Bash Question

Now I have following code in shell script, can someone help me fix it and understand the code?

I am new to shell script as well as new to stackoverflow. :) and I need some help from you guys

cat Header_list.txt
ID

cat test123123.csv
Name ID Portfolio CounterParty Cards ID Process *Theoretical Model Issue Date
MUREX_01576184 :MUREX_01576184 :HK-AUFWD H-CCBC_PM 20229 metals :BNS Forex 4/27/2016
MUREX_01576194 :MUREX_01576194 :HK-AUFWD H-CCBC_PM 20229 metals :BNS Forex 4/27/2016
MUREX_02351193 :MUREX_02351193 :HK-AUFWD H-CCBC_PM 20229 metals :BNS Forex 10/31/2016
MUREX_02351197 :MUREX_02351197 :HK-AUFWD H-CCBC_PM 20229 metals :BNS Forex 10/31/2016
MUREX_02351203 :MUREX_02351203 :HK-AUFWD H-CCBC_PM 20229 metals :BNS Forex 10/31/2016
MUREX_02351207 :MUREX_02351207 :HK-AUFWD H-CCBC_PM 20229 metals :BNS Forex 10/31/2016



# here is the main code
awk 'NR==FNR{
Cols=Cols (Cols?"|":"")$1
next
}

FNR==1{
for (i=1;i<=NF;i++)
if (match($i,Cols))
Ar[++n]=i
}

{
for (i=1;i<=n;i++)
printf (i<n)? $(Ar[i]) FS : $(Ar[i])
printf "\n"
}' FS="," Header_list.txt test123123.csv

Expected Output:
ID
:MUREX_01576184
:MUREX_01576194
:MUREX_02351193
:MUREX_02351197
:MUREX_02351203
:MUREX_02351207

Actual
ID Cards ID
:MUREX_01576184 20229
:MUREX_01576194 20229
:MUREX_02351193 20229
:MUREX_02351197 20229
:MUREX_02351203 20229
:MUREX_02351207 20229


In the example, it returns all the columns which the header contains "ID". I am thinking it's the NR==FNR part causing the problem. But I couldn't understand what it means by

Cols=Cols (Cols?"|":"")$1
next


Could someone help me understand the code? If possible, fix the code too?

Answer

The problem is a match method that takes a regex as a second argument.

Change this if to normal comparison (==) should help.

Comments