How to modify an output?

I have a list

XPAR
XPAR
XPAR    ,
XPAR    ,       ,
XPAR    ,1,
XPAR 196
XPAR 95
XPAR 95,77

This has space and tabs on the second row. I would like it to look like

XPAR 1, 196, 95, 77

But I always get the below because of the spaces above.

, , ,1, 196 95 95,77

I use awk -v x="" '{ s=s sprintf(x "%s" x " ", $2) } END { sub(",$2", "", s); print(s) }' sources3 > TMPFILE

Please Help

awk -F'[, ]' ' p ~ $1 {
                        for(i=2;i<=NF;i++) {
                                if($i != "")
                                        printf ",%s",$i;
                        }
                } p !~ $1 {
                        printf "%s", $1;
                } {
                        p = $1;
                } END {
                        printf "\n";
}' file

The script bipinajith provided puts out a <comma> before the first XPAR value, uses <comma> instead of <comma><space> to separate XPAR values, and outputs duplicated XPAR values multiple times. I think the following produces the requested output:

awk -F '[ \t,]+' '
{	for(i = 2; i <= NF; i++)
		if($i != "" && (! ($i in x))) {
			x[$i]
			printf("%s %s", oc++ ? "," : "XPAR", $i)
		}
}
END {	printf("\n")}' file

If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk .

Thank you Don...It works !!!

OR Try

$ awk '{gsub(","," ");
    for(i=2;i<=NF;i++){
    if(!A[$i]++){
    X[$1]=X[$1]?X[$1]","$i:$1" "$i}}}
    END{print X[$1]}' file

XPAR 1,196,95,77

I would like to also not display the table which contains no info on the second column.
Now i have:

XPAR
XPAR
XPAR ,
XPAR , ,
XPAR ,1,
XPAR 196
XPAR 95
XPAR 95,77

I would like
XPAR 1
XPAR 95,77
XPAR 196
...
Just the lines that contain a data on the second column.

Please check this thread

I've already answered there...:slight_smile:

Hi Pamu
I tried both and they still dint work for me :frowning:

why XPAR 95 line is not printed.

Please provide some details.

That should be there too ... oops
I just put ... to denote etc etc.
:slight_smile: