如何导出当前日期和时间并在包含"Hello"的每一行的末尾附加 [英] How to derive current date and time and append at the end of each line that contains 'Hello'

查看:79
本文介绍了如何导出当前日期和时间并在包含"Hello"的每一行的末尾附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件party.txt,其中包含以下内容:

I have the following file party.txt that contains something like the following:

Hello Jacky
Hello Peter
Bye Johnson
Hello Willy
Bye Johnny
Hello Mary
Hello Wendy

我使用grep hello捕获内容,但是当我使用print date +"%Y-%m-%d'并将其追加到内容时,我将无法捕获,并且每行会得到很多0.

I used the grep hello to capture the contents, but when I use the print date +"%Y-%m-%d' and append to it, I cannot, and I will get many 0 per line.

cat party.txt | grep Hello | awk '{ print date +"%Y-%m-%d"}'

我可能会缺少什么?

推荐答案

使用awk的一种方法:

awk -v date="$(date +"%Y-%m-%d %r")" '/Hello/ { print $0, date}' party.txt

结果:

Hello Jacky 2012-09-11 07:55:51 PM
Hello Peter 2012-09-11 07:55:51 PM
Hello Willy 2012-09-11 07:55:51 PM
Hello Mary 2012-09-11 07:55:51 PM
Hello Wendy 2012-09-11 07:55:51 PM

请注意,日期值仅在awk开始时设置,因此即使命令运行很长时间也不会更改.

Note that the date value is only set when awk starts, so it will not change, even if the command takes a long time to run.

这篇关于如何导出当前日期和时间并在包含"Hello"的每一行的末尾附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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