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
Yoda
February 20, 2013, 11:34am
2
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 !!!
pamu
February 21, 2013, 5:27am
5
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.
pamu
February 21, 2013, 6:01am
7
Please check this thread
I've already answered there...
Hi Pamu
I tried both and they still dint work for me
pamu
February 21, 2013, 8:21am
9
priyanka.premra:
XPAR
XPAR
XPAR ,
XPAR , ,
XPAR ,1,
XPAR 196
XPAR 95
XPAR 95,77
I would like
XPAR 1
XPAR 95,77
XPAR 196
...
why XPAR 95 line is not printed.
Please provide some details.
That should be there too ... oops
I just put ... to denote etc etc.