我是shell脚本的新手,也是stackoverflow的新手。 :)我需要你们的帮助
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
在该示例中,它返回标题包含“ID”的所有列。我在想这是导致问题的NR == FNR部分。但我无法理解
的含义 Cols=Cols (Cols?"|":"")$1
next
有人可以帮我理解代码吗?如果可能,还要修复代码吗?
答案 0 :(得分:1)
问题是match
方法,它将正则表达式作为第二个参数。
如果正常比较(==
)应该有帮助,请更改此项。