Unix grep 一个字符串然后只显示列表中的字符串
谁能帮我看看如何做到这一点。
我使用 netstat 找出当前有多少服务器连接到我的服务器,我得到了以下列表,server001 和 002 有许多服务连接到 myserver01
myserver01.1050 server001.com.51535 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.36565 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.35262 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.41700 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.36525 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.54575 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.44401 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.47922 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.57080 49680 0 49680 0 ESTABLISHED
谁能告诉我如何 grep 或排序列表,其中我可以获得一个唯一的服务器名称,如下所示,
myserver01.1050 server001.com
myserver01.1050 server002.com
谢谢
Can anyone help to show me how to do this.
I use netstat to find out how many servers are currently connected to my servers, and I've got the following list, server001 and 002 has a number of services connected to myserver01
myserver01.1050 server001.com.51535 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.36565 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.35262 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.41700 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.36525 64860 0 49680 0 ESTABLISHED
myserver01.1050 server002.com.54575 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.44401 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.47922 64860 0 49680 0 ESTABLISHED
myserver01.1050 server001.com.57080 49680 0 49680 0 ESTABLISHED
Can anyone tell me how can I grep or sort the list , which I could get a unique server name, something like the one below
myserver01.1050 server001.com
myserver01.1050 server002.com
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
并使用
uniq -c
您也可以获得计数and using
uniq -c
you can get a count as well这有点难看,但它有效(
6333197.txt
包含 netstat 输出):-F
定义输入字段分隔符,(awk 手册页)This is a little ugly, but it works (
6333197.txt
containing your netstat output):-F
defines the input field separator, (awk man page)这一切都可以在 1 行 awk 命令中完成,如下所示:
输出(使用上述输入):
It can all be done in 1 line awk command like this:
OUTPUT (with above input):
试试这个:
虽然我无法判断您是否只需要服务器名称或仅需要一对名称。有点猜测,如果我误解了这个问题,我很抱歉。
Try this:
Although I can't tell whether you want only the server name or only the pair of names. Kind of guessing, so sorry if I misinterpreted the question.
这是使用
sed
和sort -u
的版本:分别。使用 GNU 版本的
sed
和sort -u
:That's a version using
sed
andsort -u
:resp. with GNU version of
sed
anssort -u
:使用
sed
和sort
:Using
sed
andsort
: