从匹配字符串的最后一行截断shell命令输出直到EOF

时间:2017-09-11 15:12:36

标签: linux shell awk tail

我想截断shell脚本的输出,只打印匹配字符串/正则表达式和EOF的最后一行之间的部分。

例如,当运行letsencrypt certbot renew --post-hook "service apache2 reload; service nginx reload"时,我得到类似

的内容
...
http-01 challenge for domain1.tld
Waiting for verification...
Cleaning up challenges

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/domain1.tld/fullchain.pem
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/domain2.tld
-------------------------------------------------------------------------------
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for domain.tld
http-01 challenge for www.domain2.tld
Waiting for verification...
Cleaning up challenges

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/domain2.tld/fullchain.pem
-------------------------------------------------------------------------------
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/domain1.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain2.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain3.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain4.tld/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
Running post-hook command: service apache2 reload; service nginx reload
Output from service:
 * Reloading web server apache2
 * 
 * Reloading nginx configuration nginx
   ...done.

现在我想拥有的是最后-------行之后的所有内容,因此所需的结果将是

** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/domain1.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain2.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain3.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain4.tld/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
Running post-hook command: service apache2 reload; service nginx reload
Output from service:
 * Reloading web server apache2
 * 
 * Reloading nginx configuration nginx
   ...done.

我考虑了一些tail -n 15命令,但每次添加新域时我都不想调整脚本。

感谢您的帮助!

编辑:与此同时,我找到了一个自己的解决方案,这个解决方案不如@ anubhava的

cnt1=`grep -n "\----" certbot.log | tail -n1 | awk -F : '{ print $1 }'`
cnt2=`wc -l certbot.log | awk '{ print $1 }'`
cnt3=$((cnt2-cnt1))
tail -n $cnt3 certbot.log

1 个答案:

答案 0 :(得分:1)

您可以使用awk

awk '/^-{6}/{p=1; str=""; next} p{str = str $0 ORS} END{printf "%s", str}' file

此awk命令将------与任何行中的起始文本匹配,一旦找到它,我们将标记p设置为1并将缓冲区str初始化为空。接下来,如果设置了标志str,我们会不断将每行添加到缓冲区p中。

请注意,如果我们遇到另一个------,那么我们会将str重新初始化为空,从而保留最后匹配的匹配项。

<强>输出:

** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/domain1.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain2.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain3.tld/fullchain.pem (success)
  /etc/letsencrypt/live/domain4.tld/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
Running post-hook command: service apache2 reload; service nginx reload
Output from service:
 * Reloading web server apache2
 *
 * Reloading nginx configuration nginx
   ...done.

替代解决方案是在tac之前和之后使用awk并仅打印第一场匹配:

tac file | awk '/^-{6}/{exit} 1' | tac