0

Currently my csv input looks like below:

network_name,network_set
P1MSVmgmtvM,Data_NetworkSet_A
P1MSVvMotion,Data_NetworkSet_A
P2MSVmgmtvM,Data_NetworkSet_B
P2MSVvMotion,Data_NetworkSet_B
E1MSVEDGEiDMZRUE1,Edge_NetworkSet_A
E1MSVEDGEiEXPRUE1,Edge_NetworkSet_A

I want output like below: (for each network set, I want to display its associated network name)

Data_NetworkSet_A
 P1MSVmgmtvM
 P1MSVvMotion
Data_NetworkSet_B
 P2MSVmgmtvM
 P2MSVvMotion
Edge_NetworkSet_A
 E1MSVEDGEiDMZRUE1
 E1MSVEDGEiEXPRUE1
  • 1
    ...and what have you tried this far? – maulinglawns Apr 14 '18 at 11:31
  • Actually it was a big csv file with lot of column values. I was able to seperate out mentioned network_set and network_name column using processLine () function, but could not understand the logic , how to compare and achieve desired output by ignoring duplicate network_set – Prajith P Apr 14 '18 at 11:37
0

You could use awk:

$ awk -F, 'NR>1{seen[$2]=seen[$2]"\n "$1;} END{for(x in seen) print x, seen[x]}' infile
Data_NetworkSet_A
 P1MSVmgmtvM
 P1MSVvMotion
Data_NetworkSet_B
 P2MSVmgmtvM
 P2MSVvMotion
Edge_NetworkSet_A
 E1MSVEDGEiDMZRUE1
 E1MSVEDGEiEXPRUE1

In seen[$2]=seen[$2]"\n "$1;; means print a \newline-space, followed by first column value $1 when it has same second column seen[$2]=... and append into the same key index =seen[$2]... and save the result in same key's value.

The END statement, awk executing this block in end when all records/line read, and we used a for-loop to iterate over the array called seen and print the key first and the value of the key in next.

  • Thanks alot @afshin. You saved my day. I have a related query , if you can advice on same would be great.. – Prajith P Apr 14 '18 at 22:53
  • You are welcome! If this resolved your problem, please consider to accepting as answer. for your other question I see you have good answers received. – αғsнιη Apr 15 '18 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.