Hi All,
I have a file like
john::208:johnson
john::208:mery
john::208:test
admin::1:johnson
admin::1:test
and wanna convert this as
john::208:johnson,mery,test
admin:*:1:johnson,test
please help me to create a script for this
thanks in advance
John
jaduks
2
$ cat joh.txt
john::208:johnson
john::208:mery
john::208:test
john::207:test7
john::207:my7
admin:*:1:johnson
admin:*:1:test
$ awk -F ":" '{Arr[$1":"$2":"$3]=sprintf("%s,%s",Arr[$1":"$2":"$3],$4)} END {for ( i in Arr) {printf("%s:%s\n",i,Arr)}}' joh.txt | sed 's/:,/:/'
admin:*:1:johnson,test
john::207:test7,my7
john::208:johnson,mery,test
There could be more efficient way than this.
//Jadu
Other way:
> cat file
john::208:johnson
john::208:mery
john::208:test
admin:*:1:johnson
admin:*:1:test
juan:u:107:juan
juan:u:107:jose
kyle:l:209:kyle
>awk '
function p_f ()
{
printf("%s",pf)
for (j=0;j<=i;++j)
printf("%s",FS""a[j])
printf("\n")
i=0
}
{
f=$1FS$2FS$3
if ( f != pf && NR )
p_f()
i++
a=$NF
pf=f
}
END{
p_f()}
' FS=':' file
john::208::johnson:mery:test
admin:*:1::johnson:test
juan:u:107::juan:jose
kyle:l:209::kyle
another alternative
awk 'BEGIN{FS=":";SUBSEP="@"}
{
a[$1SUBSEP$2SUBSEP$3] =a[$1SUBSEP$2SUBSEP$3]" "$NF
}
END{
for(i in a) {
n=split(a,j," ")
printf "%s:" ,i
for( k=1;k<=n;k++) printf "%s " ,j[k]
print ""
}
}' "file"
output:
# ./test.sh
kyle@l@209:kyle
juan@u@107:juan jose
admin@*@1:johnson test
john@@208:johnson mery test
Another one:
awk 'BEGIN {FS = OFS = ":"}
!arr[$1$3] {arr[$1$3] = $0; next}
{arr[$1$3] = arr[$1$3] "," $4}
END {for(i in arr) {print arr}}
' file
Regards