简体   繁体   English

如何在跳过最后N个字符后在每行末尾添加文本

[英]How to add text at the end of each line after skipping Last N characters

I'm on Linux command line and I have log file with following content 我在Linux命令行上,我有以下内容的日志文件

Subscription Service Shutdown at 9:59PM UTC
Subscription Service Restarted at 11:57PM UTC

I want to add date before Time Stamp for every line 我想在时间戳之前为每一行添加日期

Subscription Service Shutdown at 01 Oct 2016 9:59PM UTC
Subscription Service Restarted at 01 Oct 2016 11:57PM UTC

vim方式:

:%s/ at /&knownString/

Something like this: 像这样的东西:

sed -r "s/([0-9]+{1}:[0-9]+{1}[AZ]{2}.*$)/01 Oct 2016 \\1/g" test.txt > new.txt sed -r“s /([0-9] + {1}:[0-9] + {1} [AZ] {2}。* $)/ 2016年10月1日\\ 1 / g”test.txt> new 。文本

Where text.txt is your log file and "01 Oct 2016 " is your text and the \\1 is the time inserted back in from the group match. text.txt是您的日志文件,“2016年10月1日”是您的文本,\\ 1是从组匹配中插入的时间。

you can try something like this; 你可以尝试这样的事情;

 echo "Subscription Service Shutdown at 9:59PM UTC"  | sed "s/at */at $(date '+%Y %b %d') /"

Eg; 例如;

$ echo "Subscription Service Shutdown at 9:59PM UTC"  | sed "s/at */at $(date '+%Y %b %d') /" 
Subscription Service Shutdown at 2016 Nov 08 9:59PM UTC

Different ways to do it in vim vim做不同的方法

1) add string after at 1)在at之后添加字符串

:%s/at \zs/01 Oct 2016 /


2) Find time string of the format HH:MM followed by AM UTC or PM UTC and add string. 2)找到格式为HH:MM时间字符串,后跟AM UTCPM UTC并添加字符串。 Add $ anchor if you need to match only at end of line 如果您只需要在行尾匹配,请添加$ anchor

:%s/\v(\d{1,2}:\d{1,2}[AP]M UTC)/01 Oct 2016 \1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM