如何使用 awk 第3列排序

我有一个这样的文件(user.csv)

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

要按用户打印所有列排序,

我试过 awk -F ":" '{print|"$3 sort -n"}' user.csv,没用。

204383 次浏览

sort怎么样。

sort -t, -nk3 user.csv

哪里

  • -t,-将分隔符定义为 ,

  • -n-给你数字排序。自从你添加它在你的 如果您的用户字段只是文本,那么您不需要它。

  • -k3-定义字段(key) . user 是第三个字段。

  1. 使用 awk 将用户 ID 放在前面。
  2. 排序
  3. 使用 sed 删除重复的用户 ID,假设用户 ID 不包含任何空格。

    awk -F, '{ print $3, $0 }' user.csv | sort | sed 's/^.* //'
    
awk -F, '{ print $3, $0 }' user.csv | sort -nk2

还有逆序排列

awk -F, '{ print $3, $0 }' user.csv | sort -nrk2

You can choose a delimiter, in this case I chose a colon and printed the column number one, sorting by alphabetical order:

awk -F\: '{print $1|"sort -u"}' /etc/passwd

试试这个

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

或者

sort -t',' -nk3 user.csv

为了从排序中排除第一行(头部) ,我将它分成两个缓冲区。

df | awk 'BEGIN{header=""; $body=""} { if(NR==1){header=$0}else{body=body"\n"$0}} END{print header; print body|"sort -nk3"}'
awk -F "," '{print $0}' user.csv | sort -nk3 -t ','

这个应该可以

Seeing as that the original question was on how to use awk and every single one of the first 7 answers use sort instead, and that this is the top hit on Google, here is how to use awk.

带头的示例 net.csv 文件:

ip,hostname,user,group,encryption,aduser,adattr
192.168.0.1,gw,router,router,-,-,-
192.168.0.2,server,admin,admin,-,-,-
192.168.0.3,ws-03,user,user,-,-,-
192.168.0.4,ws-04,user,user,-,-,-

然后排序 wk:

#!/usr/bin/awk -f
# usage: ./sort.awk -v f=FIELD FILE


BEGIN {
FS=","
}


# each line
{
a[NR]=$0 ""
s[NR]=$f ""
}


END {
isort(s,a,NR);
for(i=1; i<=NR; i++) print a[i]
}


#insertion sort of A[1..n]
function isort(S, A, n, i, j) {
for( i=2; i<=n; i++) {
hs = S[j=i]
ha = A[j=i]
while (S[j-1] > hs) {
j--;
S[j+1] = S[j]
A[j+1] = A[j]
}
S[j] = hs
A[j] = ha
}
}

使用方法:

awk sort.awk f=3 < net.csv  # OR


chmod +x sort.awk
./sort.awk f=3 net.csv

GNU awk:

awk -F ',' '{ a[$3]=$0 } END{ PROCINFO["sorted_in"]="@ind_str_asc"; for(i in a) print a[i] }' file

有关更多排序算法,请参见 8.1.6使用预定义的数组扫描顺序和 gawk

我用 mawk 运行 Linux (Ubuntu) :

tmp$ awk -W version
mawk 1.3.4 20200120
Copyright 2008-2019,2020, Thomas E. Dickey
Copyright 1991-1996,2014, Michael D. Brennan


random-funcs:       srandom/random
regex-funcs:        internal
compiled limits:
sprintf buffer      8192
maximum-integer     2147483647

Mawk (和 发呆)有一个将 print的输出重定向到命令的选项:

The output of print and printf can be redirected to a file or command by appending > file, >> file or | command to the end of the print statement. Redirection opens file or command only once, subsequent redirections append to the already open stream.

Below you'll find a simplied example how | can be used to pass the wanted records to an external program that makes the hard work. This also nicely encapsulates everything in a single awk file and reduces the command line clutter:

tmp$ cat input.csv
alpha,num
D,4
B,2
A,1
E,5
F,10
C,3
tmp$ cat sort.awk
# print header line
/^alpha,num/ {
print
}


# all other lines are data lines that should be sorted
!/^alpha,num/ {
print | "sort --field-separator=, --key=2 --numeric-sort"
}
tmp$ awk -f sort.awk input.csv
alpha,num
A,1
B,2
C,3
D,4
E,5
F,10

See man sort for the details of the sort options:

-t, --field-separator=SEP
use SEP instead of non-blank to blank transition
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
-n, --numeric-sort
compare according to string numerical value