AWK脚本在getent passwd中按用户名的字母顺序对列表进行排序

时间:2019-03-04 07:18:31

标签: sorting awk pipeline

我对awk非常陌生,我正在尝试按字母顺序对第五组用户名进行排序,该用户名的名称为“ Zack e”。我在终端中输入了import pandas as pd data = [{"Text" : "Dog", "Dog" : 1}, {"Text" : "Cat", "Dog" : 0}, {"Text" : "Mouse", "Dog" : 0}, {"Text" : "Dog", "Dog" : 1}] df = pd.DataFrame(data) mykewords = ['Cat', 'Mouse'] for i in mykewords: df[i] = df.Text.str.count(i)

getent passwd

我也尝试过这种方法

zack:x:115:120:Zack E:/home/zack:/var/run/bin/bash/false
hp:x:118:7:HPLIP system user:/var/run/hplip:/bin/false
armvad:x:3:3:Ezikon Armvad:/dev:/usr/sbin/nologin
bruh:x:1542:1546:Burh RG:/home/banner:/bin/bash

但是它所做的只是排序第一组而不是所需的组。如何将我的方法与awk脚本结合起来,我在awk中所要做的就是for i in `sed -e 's/.* \(\d\)*/\1/' passwd.in | sort`; do grep $i passwd.in; done > file_sort.txt

有什么办法,我可以重新排列令牌,以便排序可以正常工作,然后将它们放回原位?

2 个答案:

答案 0 :(得分:1)

为什么“排序”可以优雅地完成这项工作?

I want to ____ the car because it is cheap.

或者相反

sort -t : -k5 myFile.txt

答案 1 :(得分:1)

$5对于每条记录都是唯一的时候,使用GNU awk和for scanning order在输出中进行排序:

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

输出:

bruh:x:1542:1546:Burh RG:/home/banner:/bin/bash
armvad:x:3:3:Ezikon Armvad:/dev:/usr/sbin/nologin
hp:x:118:7:HPLIP system user:/var/run/hplip:/bin/false
zack:x:115:120:Zack E:/home/zack:/var/run/bin/bash/false

解释:

$ awk -F: '{                              # set field delimiter
     a[$5]=$0                             # hash records, use $5 as key
}
END {
    PROCINFO["sorted_in"]="@ind_str_asc"  # set the for traverse order
    for(i in a)                           # use it
        print a[i]                        # output
}' file

由于哈希键是$5,因此它必须唯一以避免冲突。如果它们不是唯一的,那么我想在这种情况下,如果记录是唯一的,则可以:

$ awk -F: '{
    a[$0]=$5                              # changed
}
END {
    PROCINFO["sorted_in"]="@val_str_asc"  # changed
    for(i in a)
        print i                           # changed
}' file

然后当然有函数asortasorti对数组进行排序,如上面的示例在输出中进行排序。例如,我们希望这5美元不会是唯一的:

$ awk -F: '{
    a[$0]=$5                      # record is unique, $5 is not expected to be
}
END {
    n=asorti(a,b,"@val_str_asc")  # asorti to preserve $0 in b but order from a[] value
    for(i=1;i<=n;i++)             # sorted to b, index rewritten from 1..n
        print i, b[i]
}' file

现在输出:

1 bruh:x:1542:1546:Burh RG:/home/banner:/bin/bash
2 armvad:x:3:3:Ezikon Armvad:/dev:/usr/sbin/nologin
3 hp:x:118:7:HPLIP system user:/var/run/hplip:/bin/false
4 zack:x:115:120:Zack E:/home/zack:/var/run/bin/bash/false