awkcontrol-m

awk can not print an array and a position parameter at the same time


awk can not print an array and a position parameter at the same time

hehe.txt —> 23402269,55
haha.txt —> 23402269,108147883084318723015500000055,1,0

--

Can not get a[$1] value

awk -F"," '{if(NR==FNR){a[$1] = $2}else{if($1 in a){print a[$1]" | "$0}}}' hehe.txt haha.txt

or

awk -F"," '{if(NR==FNR){a[$1] = $2}else{if($1 in a){printf("%s | %s\n",a[$1],$0)}}}' hehe.txt haha.txt

result

| 23402269,108147883084318723015500000055,1,0

--

using printf with %d, I can get correct result

awk -F"," '{if(NR==FNR){a[$1] = $2}else{if($1 in a){printf("%d | %s\n",a[$1],$0)}}}' hehe.txt haha.txt

result

55 | 23402269,108147883084318723015500000055,1,0

--

Question: I wonder why, thanks.


Solution

  • As mentioned check if there are any control M characters if you need to remove them use following and then run your command.(In case you have dos2unix utility run it on your Input_file and get rid of control M characters)

    tr -d '\r' < Input_file > temp_file && mv temp_file Input_file