【问题标题】:ONELINER which print system groups and their identifiers, for all groups whose identifiers start with '1'ONELINER 打印系统组及其标识符,用于标识符以“1”开头的所有组
【发布时间】:2014-11-22 16:53:31
【问题描述】:

我必须为标识符以“1”开头的所有组编写一个打印系统组及其标识符的 ONELINER。

这些是我系统中组的一些示例:

fuse:x:105:
messagebus:x:106:
ssl-cert:x:107:
lpadmin:x:108:piciu
scanner:x:109:saned
mlocate:x:110:
ssh:x:111:
utempter:x:112:
avahi-autoipd:x:113:
rtkit:x:114:
saned:x:115:
whoopsie:x:116:
avahi:x:117:
lightdm:x:118:
nopasswdlogin:x:119:
bluetooth:x:120:
colord:x:121:
pulse:x:122:
pulse-access:x:123:
piciu:x:1000:
sambashare:x:124:piciu
howard:x:1001:
penny:x:1002:
leonard:x:1003:
sheldon:x:1004:

我必须打印:fuse 105 messagebus 106 ssl-cert 107 etc etc etc leonard 1003 sheldon 1004

【问题讨论】:

    标签: linux bash unix ubuntu


    【解决方案1】:

    你可以使用一个非常简单的awk脚本作为

    $ awk -F: '$3 ~ /^1/' input
    fuse:x:105:
    messagebus:x:106:
    ssl-cert:x:107:
    lpadmin:x:108:piciu
    scanner:x:109:saned
    mlocate:x:110:
    ssh:x:111:
    utempter:x:112:
    avahi-autoipd:x:113:
    rtkit:x:114:
    saned:x:115:
    whoopsie:x:116:
    avahi:x:117:
    lightdm:x:118:
    nopasswdlogin:x:119:
    bluetooth:x:120:
    colord:x:121:
    pulse:x:122:
    pulse-access:x:123:
    piciu:x:1000:
    sambashare:x:124:piciu
    howard:x:1001:
    penny:x:1002:
    leonard:x:1003:
    sheldon:x:1004:
    awk -F: '$3 ~ /^1/' input
    fuse:x:105:
    messagebus:x:106:
    ssl-cert:x:107:
    lpadmin:x:108:piciu
    scanner:x:109:saned
    mlocate:x:110:
    ssh:x:111:
    utempter:x:112:
    avahi-autoipd:x:113:
    rtkit:x:114:
    saned:x:115:
    whoopsie:x:116:
    avahi:x:117:
    lightdm:x:118:
    nopasswdlogin:x:119:
    bluetooth:x:120:
    colord:x:121:
    pulse:x:122:
    pulse-access:x:123:
    piciu:x:1000:
    sambashare:x:124:piciu
    howard:x:1001:
    penny:x:1002:
    leonard:x:1003:
    sheldon:x:1004:
    

    它有什么作用?

    • -F: 将字段分隔符设置为:

    • '$3 ~ /^1/' 检查第三个字段 id 是否以1开头

    【讨论】:

    • @anubhava 谢谢 :)
    • 非常感谢!!我发现了一些关于 awk 命令的东西,但我不知道如何实现。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多