[英]How to format output of a user account from /etc/passwd
I want to format output of a user account from /etc/passwd
to display only the name, role, and directory path, all separated by commas. 我想从/etc/passwd
格式化用户帐户的输出,以仅显示名称,角色和目录路径,所有这些都用逗号分隔。 I know this should be easy, but for the life of me I cannot figure out how to display between certain colons. 我知道这应该很容易,但是对于我自己的一生,我无法弄清楚如何在某些结肠之间显示。 (Note this should work with any username, not just the ex) (请注意,这应该适用于任何用户名,而不仅限于前者)
Ex of grep joe /etc/passwd
: 例如grep joe /etc/passwd
:
joe:x:1001:1001:System Admin:/home/joe:/bin/bash
Desired Output : 所需输出 :
joe, System Admin, /home/joe
Thank you! 谢谢!
awk 'BEGIN{ FS=":"; OFS=", " } $1=="joe" { print $1,$5,$6; exit }' /etc/passwd
(但是下次您应该付出更多的努力-您的问题非常令人讨厌:)
使用cut
将逗号用作--output-delimiter
:
cut -d: -f1,5,6 --output-delimiter=, /etc/passwd
Try: 尝试:
grep joe /etc/passwd | cut -d: -f1,5,6
If you really need "," as a delimiter: 如果确实需要“,”作为分隔符:
grep "^joe:" /etc/passwd | cut -d: -f1,5,6 | tr : ,
The "^" ensures only matches to "joe" at the beginning of the line are intercepted (Thanks PSkocik for the reminder). “ ^”确保仅截取行开头与“ joe”的匹配项(感谢PSkocik进行提醒)。 grep
by defaults accepts a regex
. grep
默认情况下接受regex
。
Translation in bash, but two lines: 用bash进行翻译,但有两行内容:
x=`grep joe /etc/passwd | cut -d: -f1,5,6`
echo ${x/:/,}
Adding to the variations, in bash itself, you can pipe the output of grep
directly to a brace enclosed read
and separate the fields using IFS
as required. 除了变化之外,在bash本身中,您可以将grep
的输出直接传递到括号括起来的read
并根据需要使用IFS
分隔字段。 Using short names for the fields, you could do: 使用字段的短名称,您可以执行以下操作:
grep '^joe:' /etc/passwd |
{ IFS=: read -r u x uid gid n h s; echo "$u, $n, $h"; }
Which would give the output you seek (if /etc/passwd
contained the entry) 它将给出您想要的输出(如果/etc/passwd
包含条目)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.