Eval in awk

Hi

I am trying to remove duplicates on keys in a file and so far the below seems to work

sort -t\| -k2,4 input.txt| awk -F'|' '{if (NR==1) print $0} {x=$2 $3 $4} NR>1 {if ($2 $3 $4 != y) {print $0}} {y=x}'
and now I want to pass comma seperated column number list to the script and use shell variable to hold the same and pass that to awk using the below, but it doesnt seem to work.

z=$1

echo \$$z|sed s/','/' $'/g

collist=`echo $2|sed s/'.'/' $'`

sort -t\| -k2,4 input.txt| awk -v awkcolist="$collist" -F'|' '{if (NR==1) print $0} {x=print awkcolist} NR>1 {if ($2 $3 $4 != y)  {print $0}} {y=x}'

The reason is awkcolist is evaluated to '$2 $3 $4' and not the actual values of those columns.

Please help

[Code tags doesnt seem to work for some reason on my browser at this point of time]

Thanks

-Zulfi

Without samples it's difficult to guess what you want to do. How about removing the $-signs from awkcolist, splitting it into an array, and using a for loop to run through the array elements to construct your x variable?

Hi,

In the above column 2,3,4 of the file input.txt are key columns and I have to remove duplicates on these columns. Once that is done I need to make the script generic so that I will accept comma seperated list of columns and file name and script should remove duplicates over the keys.
The first code fragment is working fine when the key column numbers are har coded inside awk but when I try to pass the column list as a variable trying to imitate the first code fragment it isn't working.

You do not need browser support for this basic, simple, easy feature.

Type the letters [ , c , o , d , e , ] , ! , [ , / , c , o , d , e , ]

Voila, you have a ! inside code tags, like this:

!
1 Like