Sample Header Ad - 728x90

duplicated entries of an array in awk

3 votes
1 answer
321 views
I have a file with 4 columns. When I put these 4 columns into an array using NR as the index, the entries are duplicated somehow. See below for an elaboration of the issue. The first 5 lines of the file look like this -bash-4.2$ cat -ve file | head -n 5 chr start end p$ 13 59341171 59343427 1.86642E-18$ 10 72886545 72888679 1.13636E-09$ 16 81900987 81902805 6.79697E-09$ 1 46797890 46800143 2.24436E-08$ I assigned each line as an entry of an array indexed by the NR, then the print out of the array looks like this (using the first 5 lines as an example): -bash-4.2$ awk 'NR for(x in a) > print x, a[x]}' file 1 chr start end p 1 chr start end p 2 13 59341171 59343427 1.86642E-18 1 chr start end p 2 13 59341171 59343427 1.86642E-18 3 10 72886545 72888679 1.13636E-09 4 16 81900987 81902805 6.79697E-09 1 chr start end p 2 13 59341171 59343427 1.86642E-18 3 10 72886545 72888679 1.13636E-09 4 16 81900987 81902805 6.79697E-09 5 1 46797890 46800143 2.24436E-08 1 chr start end p 2 13 59341171 59343427 1.86642E-18 3 10 72886545 72888679 1.13636E-09 I can see that the 5 lines of the file are there, but entries are duplicated a few times. I wonder what the problem is and how to fix it. Thanks in advance.
Asked by Xuan (45 rep)
Apr 3, 2023, 12:01 PM
Last activity: Apr 3, 2023, 12:07 PM