将一个命令的输出作为参数传递给另一个 [英] Passing output from one command as argument to another

查看:94
本文介绍了将一个命令的输出作为参数传递给另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个用于:

for i in `ls -1 access.log*`; do tail $i |awk {'print $4'} |cut -d: -f 1 |grep - $i > $i.output; done

ls 将提供access.log,access.log.1,access.log.2等.
tail (尾部)将给我每个文件的最后一行,如下所示:192.168.1.23 - - [08/Oct/2010:14:05:04 +0300]等.等等.
awk + ​​cut 将提取日期(2010年10月8日-但在每个access.log中不同),这将允许我为其 grep 并将输出重定向到一个单独的文件.

ls will give access.log, access.log.1, access.log.2 etc.
tail will give me the last line of each file, which looks like: 192.168.1.23 - - [08/Oct/2010:14:05:04 +0300] etc. etc. etc
awk+cut will extract the date (08/Oct/2010 - but different in each access.log), which will allow me to grep for it and redirect the output to a separate file.

但是我似乎无法将awk + ​​cut的输出传递给grep.

But I cannot seem to pass the output of awk+cut to grep.

所有这些的原因是那些访问日志包含具有多个日期(06/Oct,07/Oct,08/Oct)的行,而我只需要具有最新日期的行.

The reason for all this is that those access logs include lines with more than one date (06/Oct, 07/Oct, 08/Oct) and I just need the lines with the most recent date.

我该如何实现?

谢谢.

推荐答案

作为注释,tail显示最后10行.

As a sidenote, tail displays the last 10 lines.

可能的解决方案是这样grep:

A possible solution would be to grepthis way:

for i in `ls -lf access.log*`; do grep $(tail $i |awk {'print $4'} |cut -d: -f 1| sed 's/\[/\\[/') $i > $i.output; done

这篇关于将一个命令的输出作为参数传递给另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆