Unix grep 一个字符串然后只显示列表中的字符串

发布于 2024-11-15 05:39:48 字数 978 浏览 1 评论 0原文

谁能帮我看看如何做到这一点。

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

栩栩如生 2024-11-22 05:39:48
$ cut -d'.' -f1-3 input | sort | uniq
myserver01.1050    server001.com
myserver01.1050    server002.com

并使用 uniq -c 您也可以获得计数

$ cut -d'.' -f1-3 input | sort | uniq -c
      5 myserver01.1050    server001.com
      4 myserver01.1050    server002.com
$ cut -d'.' -f1-3 input | sort | uniq
myserver01.1050    server001.com
myserver01.1050    server002.com

and using uniq -c you can get a count as well

$ cut -d'.' -f1-3 input | sort | uniq -c
      5 myserver01.1050    server001.com
      4 myserver01.1050    server002.com
枕梦 2024-11-22 05:39:48

这有点难看,但它有效(6333197.txt 包含 netstat 输出):

$ cat 6333197.txt | awk -F '.' '{print $1"."$2"."$3}' | sort -u

-F 定义输入字段分隔符,(awk 手册页

This is a little ugly, but it works (6333197.txt containing your netstat output):

$ cat 6333197.txt | awk -F '.' '{print $1"."$2"."$3}' | sort -u

-F defines the input field separator, (awk man page)

深海不蓝 2024-11-22 05:39:48
cat netstat.txt | tr -s ' ' | cut -d" " -f2 | cut -d. -f1 | sort -u
cat netstat.txt | tr -s ' ' | cut -d" " -f2 | cut -d. -f1 | sort -u
我不咬妳我踢妳 2024-11-22 05:39:48

这一切都可以在 1 行 awk 命令中完成,如下所示:

awk '{gsub(/\.[0-9]*$/, "", $2); serv[$2]=$1;} END{n=asorti(serv, dest); for (i = 1; i <= n; i++) print serv[dest[i]] " " dest[i];}' file.txt

输出(使用上述输入):

myserver01.1050 server001.com
myserver01.1050 server002.com

It can all be done in 1 line awk command like this:

awk '{gsub(/\.[0-9]*$/, "", $2); serv[$2]=$1;} END{n=asorti(serv, dest); for (i = 1; i <= n; i++) print serv[dest[i]] " " dest[i];}' file.txt

OUTPUT (with above input):

myserver01.1050 server001.com
myserver01.1050 server002.com
晨曦÷微暖 2024-11-22 05:39:48

试试这个:

  $ grep myserver01.1050 logfile | \
    grep ' server' | \
    awk -F\. '{print $1, $2}' | \
    sort -u

虽然我无法判断您是否只需要服务器名称或仅需要一对名称。有点猜测,如果我误解了这个问题,我很抱歉。

Try this:

  $ grep myserver01.1050 logfile | \
    grep ' server' | \
    awk -F\. '{print $1, $2}' | \
    sort -u

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.

惯饮孤独 2024-11-22 05:39:48

这是使用 sedsort -u 的版本:

sed 's/^\(\S\S*\s\s*\S\S*\)\.[0-9][0-9]*\s.*/\1/' filename | sort -u

分别。使用 GNU 版本的 sedsort -u

sed -r 's/^(\S+\s+\S+)\.[0-9]+\s.*/\1/' filename | sort -u

That's a version using sed and sort -u:

sed 's/^\(\S\S*\s\s*\S\S*\)\.[0-9][0-9]*\s.*/\1/' filename | sort -u

resp. with GNU version of sed ans sort -u:

sed -r 's/^(\S+\s+\S+)\.[0-9]+\s.*/\1/' filename | sort -u
日裸衫吸 2024-11-22 05:39:48

使用 sedsort

sed 's/\(.*\)\..*/\1/'  filename | sort -u

Using sed and sort:

sed 's/\(.*\)\..*/\1/'  filename | sort -u
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文