我有以下示例文件:
root:server1:2014-12-12:x:Open:adm bin daemon lp mail other root sys users
sectmp:server1:2014-12-12:x:Open:users
smbnull:server1:2014-12-12:x:Locked:ids
sav:server1:2014-12-12:x:Locked:sav users
sshd:server1:2014-12-12:x:Locked:sshd
sys:server1:2014-12-12:x:Locked:sys
systemp:server1:2014-12-12:x:Open:bin sys users
我想创建一个新文件,将属于多个组的每个登录帐户放在单独的行中,例如:
root:server1:2014-12-12:x:Open:adm
root:server1:2014-12-12:x:Open:bin
root:server1:2014-12-12:x:Open:daemon
root:server1:2014-12-12:x:Open:lp
root:server1:2014-12-12:x:Open:mail
root:server1:2014-12-12:x:Open:other
root:server1:2014-12-12:x:Open:root
root:server1:2014-12-12:x:Open:sys
root:server1:2014-12-12:x:Open:users
sectmp:server1:2014-12-12:x:Open:users
smbnull:server1:2014-12-12:x:Locked:ids
sav:server1:2014-12-12:x:Locked:sav
sav:server1:2014-12-12:x:Locked:users
sshd:server1:2014-12-12:x:Locked:sshd
sys:server1:2014-12-12:x:Locked:sys
systemp:server1:2014-12-12:x:Open:bin
systemp:server1:2014-12-12:x:Open:sys
systemp:server1:2014-12-12:x:Open:users
答案1
可以使用以下方法完成awk
:
awk -F: -v OFS=: '{split ($NF, groups, " "); $NF=""; for (i in groups) {printf "%s%s\n", $0, groups[i]}}'
分配给一个字段(例如在 中$NF=""
)会导致$0
的实例被FS
替换为OFS
,因此我们必须分配:
给OFS
。不要期望输出有任何顺序 - 组字段将随机输出。sort
如果需要,请稍后再使用。