hi all i have an example i want one help me to understand cause i tried to test it but almost fail and i don't know how can i solve this problem " the main idea to read from two files and replace something from one to another " but i don't understand why it fail all time
$ cat main.txt
512|1241503759|ax|90
234|1241503760|ay|10
122|1241503823|az|90
123|1241503947|at|80
$ cat id.txt
122|US
123|IN
125|NZ
234|HK
512|ZM
600|KR
$ awk '
BEGIN {FS=OFS="|"}
FNR==NR{a[$1]=$2;next}
$1 in a{print a[$1],$2,$3,$4}
' id.txt main.txt
Output:
ZM|1241503759|ax|90
HK|1241503760|ay|10
US|1241503823|az|90
IN|1241503947|at|80
i tried this
awk 'BEGIN { FS=OFS="|" } FNR==NR{ a[$1]=$2;next } $1 in a { print a[$1] }' id.txt main.txt
awk: syntax error near line 1
awk: bailing out near line 1
when i remove " $1 in a "
awk 'BEGIN { FS=OFS="|" } FNR==NR{ a[$1]=$2;next } { print a[$1] }' id.txt main.txt
it prints spaces
i want one help me to explain this thing plz