Linux的sed命令用单引号包裹YAML日期字符串。

huangapple go评论49阅读模式
英文:

Linux sed command to wrap yaml date string with single quotes

问题

我有一些包含YAML格式的文件,其中我需要在某些日期字段的值周围添加单引号。

例如:

pubDate: 2016-10-10T08:22:00.000Z

应该更新为

pubDate: '2016-10-10T08:22:00.000Z'

我认为可以使用类似sed的工具来完成这个任务。我该如何实现?

英文:

I have some files containing yaml where I need to add single quotes around the values of some date fields.

For example:

pubDate: 2016-10-10T08:22:00.000Z

Should be updated to

pubDate: '2016-10-10T08:22:00.000Z'

I think this can be done using something like sed. How can I achieve this?

答案1

得分: 1

使用awk命令:

awk '/pubDate:/{ $NF="7"$NF"7"; print }'

对于文件,代码将如下所示:

awk -i inplace '{ if(/pubDate:/) $NF="7"$NF"7"; print }' 文件名
英文:

using awk

awk '/pubDate:/{$NF="7"$NF"7";print}'

for the file will look like this

awk -i inplace '{if(/pubDate:/)$NF="7"$NF"7";print}' FILE 

答案2

得分: 0

Using sed:

sed 's/^pubDate: \([-:.[:alnum:]]\+\)$/pubDate: '"'"''"'"'/'' FILE

我假设pubDate: <timestamp>是行中唯一的内容。

英文:

Using sed:

sed 's/^pubDate: \([-:.[:alnum:]]\+\)$/pubDate: '"'"''"'"'/' FILE

I assume that pubDate: <timestamp> is the only content on the line.

huangapple
  • 本文由 发表于 2023年5月6日 20:46:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定