2

I am trying to process a csv file using awk. I have five rows in my csv file. I want my column 1 & column 3 combined to be unique. Using awk I wrote

awk -F "," '{print $1,",",$2,","$3,",",$4,",",$5}' A.csv|sort|uniq >B.csv

What this code does is to have a unique row with 1 to 5 rows combined. I want my other columns to be in final csv file but sorted and uniq only by 1st and 3rd columns.

5
  • please provide example input and desired output Commented Jan 15, 2011 at 1:01
  • When you say combined based on a unique key does that mean the new combined row could have 6 elements in addition to the keys? If so, sounds like you picked the wrong tool. A Perl script using a hash table would work out nicely in this case. Commented Jan 15, 2011 at 1:09
  • @tawman awk supports associative arrays just fine. But until the OP actually provides something concrete that we can see, I doubt anybody is going to expend the time writing something up when we can only speculate what the OP really wants. Commented Jan 15, 2011 at 1:14
  • Agreed, awk has quite an in-depth language that rarely the unix user dives into that far. I did once a long time ago, but easier to use the tools you know. I did not mean to insult the power of awk! Commented Jan 15, 2011 at 1:17
  • 3
    You don't have to use all those quoted commas, by the way. awk -F, '{print $1,$2,$3}' OFS=, filename Commented Jan 15, 2011 at 3:49

1 Answer 1

2

assuming you want to keep at least one of the duplicate lines

$ more file
1 2 3 4 5 6
7 4 6 3 5 8
1 43 3 6 5 10
2 3 4 4 4 4

$ awk '(!($1$3 in u)){u[$1$3]=$0}END{for(i in u ) print u[i]}' file
7 4 6 3 5 8
1 2 3 4 5 6
2 3 4 4 4 4

If you don't want to keep duplicate lines

$ awk '($1$3 in u){delete u[$1$3];next}{u[$1$3]=$0}END{for(i in u ) print u[i]}' file
7 4 6 3 5 8
2 3 4 4 4 4
Sign up to request clarification or add additional context in comments.

1 Comment

@ghostdog...could you please explein the solution here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.