How to use awk sort by column 3

I have a file (user.csv) like this

ip,hostname,user,group,encryption,aduser,adattr 

want to print all sorting columns by user,

I tried awk -F ":" '{print|"$3 sort -n"}' user.csv , it does not work.

+43
awk
Jun 11 '13 at 15:37
source share
5 answers

How about just sort .

 sort -t, -nk3 user.csv 

Where

  • -t, - defines your separator as,.

  • -n - gives you a numerical look. Added since you added it in your attempt. If your user field is text, then you do not need it.

  • -k3 - defines the field (key). user is the third field.

+82
Jun 11 '13 at 15:39
source share
  • Use awk to set the user ID.
  • Sorting
  • Use sed to remove the duplicate user ID, provided the user IDs do not contain spaces.

     awk -F, '{ print $3, $0 }' user.csv | sort | sed 's/^.* //' 
+9
Jun 27 '14 at 7:48
source share

try it -

 awk '{print $0|"sort -t',' -nk3 "}' user.csv 

OR

 sort -t',' -nk3 user.csv 
+4
09 Oct '16 at 11:47
source share

You can choose a separator, in this case I select a colon and print column number one, sorting alphabetically.

 awk -f\: '{print $1|"sort -u"}' /etc/passwd 
+3
Aug 22 '16 at 2:23
source share
 awk -F, '{ print $3, $0 }' user.csv | sort -nk2 

and for the reverse order

 awk -F, '{ print $3, $0 }' user.csv | sort -nrk2 
+2
Jul 26 '16 at 15:57
source share



All Articles