sed 命令不适用于搜索字符串

sed 命令不适用于搜索字符串

我有一个如下所示的日志文件

2017-05-30 23:11:34,429 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:34,438 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:34,438 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:34,453 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".
2017-05-30 23:11:33,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication): 
2017-05-30 23:11:33,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626
2017-05-30 23:11:32,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:32,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:31,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".
2017-05-30 23:11:30,429 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:30,429 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:30,438 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:30,438 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:30,453 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".
2017-05-30 23:11:30,429 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:26,429 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:26,438 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:20,438 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:20,453 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".
2017-05-30 23:11:18,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:18,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:15,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication): 
2017-05-30 23:11:15,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626
2017-05-30 23:11:14,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:14,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:12,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:11,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:10,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:09,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".
2017-05-30 23:11:09,415 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address gateway/172.16.8.1:54626
2017-05-30 23:10:59,416 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:10:42,429 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:10:35,429 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:10:30,438 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]



我想使用sed起始搜索字符串 is 来搜索日志文件Invoking logout agent,直到结束搜索字符串为Failed login attempt。如果匹配条件,它将返回这些搜索字符串之间的信息。因此,可能会返回多个匹配的条件,因为它可能会打印在日志中的其他位置。但我未能使其按预期工作,但它可以在我之前使用过的时间范围内工作。这是我的sed和必需的论点

sed -n '/Invoking logout agent/,/Failed login attempt/p' 1min.txt



似乎返回了我整个日志文件块,因为返回信息的第一行没有“ Invoking logout agent”。从那里,我可以知道它无法正常工作。我可以知道我的问题是什么吗?这是我最需要的日志块

2017-05-30 23:11:33,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication): 
2017-05-30 23:11:33,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626
2017-05-30 23:11:32,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:32,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:31,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".

答案1

sed解决方案:

sed -n '/Invoking logout agent/{:a;N;/Failed login attempt/!ba;G;p;}' imin.txt

输出:

2017-05-30 23:11:33,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication): 
2017-05-30 23:11:33,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626
2017-05-30 23:11:32,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:32,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:31,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".

2017-05-30 23:11:18,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:18,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:15,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication): 
2017-05-30 23:11:15,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626
2017-05-30 23:11:14,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:14,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:12,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:11,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:10,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:09,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".

细节

  • /Invoking logout agent/- 在每条相遇线上Invoking logout agent

  • :a;- 定义标签a

  • N;- 将下一行追加到模式空间

  • /Failed login attempt/!ba;- 除非模式空间包含Failed login attempt- 跳回标签a

  • G- 在每个匹配的内容后添加换行符以提高可读性

  • p- 打印图案空间

相关内容