On 3/10/2008 7:46 PM, Scott S. McCoy wrote:
> On Mon, 10 Mar 2008 17:26:33 -0700, Tiago Peczenyj wrote:
>
>
>>On 10 mar, 21:20, Radu <raduco...@[EMAIL PROTECTED]
> wrote:
>>
>>>Hi guys,
>>>
>>>From a .csv file, I want to extract the last field (say it's $15, or, I
>>>guess, $NF) and make it like:
>>>'12345', '23456', '34567' (comma separated strings). The purpose is to
>>>use the list in a select request in the WHERE clause (WHERE field IN
>>>('12345', '23456', '34567'). The IN range would be therefore the file
>>>the AWK will sent the values to.
>>>
>>>Thanks,
>>>
>>>Radu
>>
>>try a awk file like this:
>>BEGIN{ printf("(WHERE field in ("); } { printf("'%s'",$NF); }
>>END{ printf(")"); }
>>
>>OR
>>
>>{ list[$NF]; }
>>END{
>> printf("(WHERE field in (");
>> for(i in list)
>> printf("'%s'",i);
>> printf(")");
>>}
>
>
> None of this keeps the comma separator.
>
> scott@[EMAIL PROTECTED]
> cat foo.awk
> function join(array, start, end, sep, result, i)
> {
> if (sep == "")
> sep = " "
> else if (sep == SUBSEP) # magic value
> sep = ""
> result = array[start]
> for (i = start + 1; i <= end; i++)
> result = result sep array[i]
> return result
> }
>
> BEGIN {
> # Assuming it's a comma or tab using CSV
> FS = ",|\t"
> }
> { list[NR] = $NF }
> END {
> printf "WHERE field IN ('%s')\n", join(list, 1, NR, "', '")
> }
>
> scott@[EMAIL PROTECTED]
> awk -f foo.awk
> foo,bar,baz,bin
> baz,bash,bit,foo
> bit,bash,blitz,bar
> WHERE field IN ('bin', 'foo', 'bar')
> scott@[EMAIL PROTECTED]
> cat foo.awk
>
It doesn't need to be that complicated if that's the style of input:
$ awk -F, -v q=\' '{o=o s q $NF q; s=", "}END{printf "WHERE field IN
(%s)\n",o}'
file
WHERE field IN ('bin', 'foo', 'bar')
Ed.


|