gpt4 book ai didi

regex - 根据正则表达式对文件行进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:03:38 25 4
gpt4 key购买 nike

我的测试文件中有几行

2018-05-28T17:13:08.024 {"operation":"INSERT","primaryKey":{"easy_id":1234},"subSystem":"ts\est","table":"tbl","timestamp":1527495188024}

我必须根据 timestamp 对行进行排序 field 。我使用 sed提取 timestamp并尝试使用 sed -e 's/((?<=\"timestamp\":)\d+.*?)/\1 作为第一列放置.任何人都可以帮助修复 reg exp。

现在收到错误:sed: 1: "s/((?<=\"timestamp\":)\ ...": \1 not defined in the RE .我认为错误是因为我的正则表达式。

最佳答案

您也可以使用 gawk 进行快速实现,而无需创建任何中间列等。

命令:

awk -F'"timestamp":' '{a[substr($2,1,length($2)-1)]=$0}END{asorti(a,b);for(i in b){print a[b[i]]}}' input

解释:

  • -F'"timestamp":' 你定义"timestamp":作为字段分隔符
  • {a[substr($2,1,length($2)-1)]=$0} 在文件的每一行上,您将时间戳值保存为索引,并将整行保存在关联数组
  • END{asorti(a,b);for(i in b){print a[b[i]]}} 在处理结束时,您根据索引对关联数组进行排序(时间戳)并根据排序的索引打印数组的内容。

输入:

$ more input
2018-05-28T17:15:08.026 {"operation":"DELETE","primaryKey":{"easy_id":1236},"subSystem":"ts\est2","table":"tbl1","timestamp":1527495188026}
2018-05-28T17:13:08.024 {"operation":"INSERT","primaryKey":{"easy_id":1234},"subSystem":"ts\est","table":"tbl","timestamp":1527495188024}
2018-05-28T17:14:08.025 {"operation":"UPDATE","primaryKey":{"easy_id":1235},"subSystem":"ts\est1","table":"tbl1","timestamp":1527495188025}

输出:

awk -F'"timestamp":' '{a[substr($2,1,length($2)-1)]=$0}END{asorti(a,b);for(i in b){print a[b[i]]}}' input                      
2018-05-28T17:13:08.024 {"operation":"INSERT","primaryKey":{"easy_id":1234},"subSystem":"ts\est","table":"tbl","timestamp":1527495188024}
2018-05-28T17:14:08.025 {"operation":"UPDATE","primaryKey":{"easy_id":1235},"subSystem":"ts\est1","table":"tbl1","timestamp":1527495188025}
2018-05-28T17:15:08.026 {"operation":"DELETE","primaryKey":{"easy_id":1236},"subSystem":"ts\est2","table":"tbl1","timestamp":1527495188026}

关于regex - 根据正则表达式对文件行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com