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

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

问题描述

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

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

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

我使用 grep hello 来捕获包含,但是当我使用打印日期+%Y-%m-%d'并附加到它,我不能,我会得到很多 0 每行

I used the grep hello to capture the contain 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

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

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