Useful *nix commands

Posted by nonenas on Nov 30th, 2007
2007
Nov 30

Case: You have a text file containing multiple lines of comma seperated values. Some values have duplicates. You want to get rid of them. Let’s say for example that the files is called "file.txt", the second comma seperated field may have duplicates and you want to get all the unique values of the second field.

cat file.txt|cut -d ‘,’ -f2|sort|uniq

 

  • cat : we use "cat" to get the content of the file

 

  • cut -d ‘,’ -f2: we use the switch -d ‘,’ to specify that values are delimited by commas (’,') and the switch -f2 to get the second field

 

  • sort: we use "sort" in order to sort the contents of the file

 

  • uniq: we use "uniq" in order to get only the unique values.

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]