I have a file (user.csv)like this
ip,hostname,user,group,encryption,aduser,adattr
want to print all column sort by user,
I tried awk -F ":" '{print|"$3 sort -n"}' user.csv
, it doesn't work.
The asorti() function One of the functions introduced in GNU awk, asorti(), provides the ability to sort an array by key (or index) or value. You can only sort the array once it has been populated, meaning that this action must not occur with every new record but only the final stage of your script.
Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.
How about just sort
.
sort -t, -nk3 user.csv
where
-t,
- defines your delimiter as ,
.
-n
- gives you numerical sort. Added since you added it in your attempt. If your user field is text only then you dont need it.
-k3
- defines the field (key). user is the third field.
Use sed to remove the duplicate user ID, assuming user IDs do not contain any spaces.
awk -F, '{ print $3, $0 }' user.csv | sort | sed 's/^.* //'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With