Question about formatting output using AWK

Hi everyone.

I've got this fille...

0
5000001
5000002
5000003
5000006
5000007
5000219
11000156
11003130
11003132

and this script...

#!/bin/ksh

FILE_ALERT_CONTACT_LIST=users.txt

userID=`awk -F"=" '{printf $1 ", "}' $FILE_ALERT_CONTACT_LIST`

echo "User is: " $userID

which outputs the following...

0, 5000001, 5000002, 5000003, 5000006, 5000007, 5000219, 11000156, 11003130, 11003132,

My question is, how do I remove the last comma? I'd like it to look like this...

0, 5000001, 5000002, 5000003, 5000006, 5000007, 5000219, 11000156, 11003130, 11003132

Thanks!

awk '{
    if (rec)
       printf("%s, ", rec)
    rec = $0
} END {
    print rec
}' file

Thanks for your help shamrock. Your script worked fine.

However my file has changed to the following...

0=Administrator
5000001=user1
5000002=user2
5000003=user3
5000006=user4
5000007=user5
5000219=user6
11000156=user7
11003130=user8
11003132=user9

so when I rec = $0 to rec = $1 I loose the first row. This is what I get...

5000001, 5000002, 5000003, 5000006, 5000007, 5000219, 11000156, 11003130, 11003132

Why am I loosing the first row?

Why you change rec value from $0 to $1? :confused:

awk -F= 'NR==1{printf("%s",$1);next}{printf(", %s",$1)}' file

awk -F"=" 'NR==1{printf $1} NR!=1{printf " ,"$1}' abc.txt

That's because when variable rec is zero the if statement evaluates to false and the printf() is skipped. Change it instead to test if the string is null.

awk '{
    if (rec"")
       printf("%s, ", rec)
    rec = $1
} END {
    print rec
}' file

Thanks for the help everyone. It's working beautifully now. :b:

Another way:

paste -sd, file

Nice one radoulov :b: