bash 如何修剪 UNIX who 命令的输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9384150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:36:57  来源:igfitidea点击:

How to trim the output of the UNIX who Command?

bashgnu-coreutils

提问by Mike Needham

I am working with an idea using the unix whocommand. As we all know, there does not seem to be a direct switch that gives just the username and line (terminal) info without the date and screen info... eg: the output is mneedham tty7...2012-02-19 11:26 (:0)

我正在使用 unixwho命令处理一个想法。众所周知,似乎没有直接开关只提供用户名和线路(终端)信息而没有日期和屏幕信息......例如:输出是mneedham tty7...2012-02-19 11:26 (:0)

What I am trying to get is just the mneedham tty7part. The solution needs to work no matter how long the username and terminal information.

我想要得到的只是mneedham tty7一部分。无论用户名和终端信息有多长,该解决方案都需要工作。

I tried using tr -s ' '(one space) like who | tr -s ' 'and that gave me one space between everything. Not quite what I was seeking. Tried cut -d" " -f1gets the username only. So I am hopeful someone can help me find the right command to get both bits of information.

我尝试使用tr -s ' '(一个空格)like who | tr -s ' ',这给了我所有内容之间的一个空格。不完全是我想要的。尝试cut -d" " -f1仅获取用户名。所以我希望有人能帮助我找到正确的命令来获取这两种信息。

Thanks.

谢谢。

回答by Andrew Clark

Using cut:

使用切割:

who | cut -d " " -f1,2

Using awk:

使用 awk:

who | awk '{ print ,  }'