Input file :F_Data_File.txt
V1|a1
V2|b1
V2|b2
V2|b3
V3|e1
V3|e2
V3|e3
V3|e4
Desired Output:
V1|a1
V2|b1,b2,b3
V3|e1,e2,e3,e4
Solution:
$ awk 'BEGIN {FS=OFS="|"}
{
V_Arr[$1] = ($1 in V_Arr) ? V_Arr[$1] "," $2 : $2
}
END{
for (k in V_Arr)
print (k, V_Arr[k] )
}' F_Data_File.txt
Explanation:
$ awk 'BEGIN {FS=OFS="|"}
{
V_Arr[$1] = ($1 in V_Arr) ? V_Arr[$1] "," $2"===" : "***"$2
print $1 "----->>" V_Arr[$1]
}' F_Data_File.txt
This part will return below output:
INDEX -----> VALUE
V1----->>***a1
V2----->>***b1
V2----->>***b1,b2===
V2----->>***b1,b2===,b3===
V3----->>***e1
V3----->>***e1,e2===
V3----->>***e1,e2===,e3===
V3----->>***e1,e2===,e3===,e4===
This statement works as a If else block, if column one value available in V_Arr then it will append the second column value with the existing value at same INDEX(column 1) or else it will store second column value in array using column one value as index.
V1|a1
V2|b1
V2|b2
V2|b3
V3|e1
V3|e2
V3|e3
V3|e4
Desired Output:
V1|a1
V2|b1,b2,b3
V3|e1,e2,e3,e4
Solution:
$ awk 'BEGIN {FS=OFS="|"}
{
V_Arr[$1] = ($1 in V_Arr) ? V_Arr[$1] "," $2 : $2
}
END{
for (k in V_Arr)
print (k, V_Arr[k] )
}' F_Data_File.txt
Explanation:
$ awk 'BEGIN {FS=OFS="|"}
{
V_Arr[$1] = ($1 in V_Arr) ? V_Arr[$1] "," $2"===" : "***"$2
print $1 "----->>" V_Arr[$1]
}' F_Data_File.txt
This part will return below output:
INDEX -----> VALUE
V1----->>***a1
V2----->>***b1
V2----->>***b1,b2===
V2----->>***b1,b2===,b3===
V3----->>***e1
V3----->>***e1,e2===
V3----->>***e1,e2===,e3===
V3----->>***e1,e2===,e3===,e4===
V_Arr[$1] = ($1 in V_Arr) ? V_Arr[$1] "," $2 "===" : $2"***"
This statement works as a If else block, if column one value available in V_Arr then it will append the second column value with the existing value at same INDEX(column 1) or else it will store second column value in array using column one value as index.
No comments:
Post a Comment