【发布时间】:2021-09-19 07:22:42
【问题描述】:
有没有办法使用 sed 或 awk(或其他我不知道的东西)将 RegEx 匹配从它出现在行上的位置移动或复制到同一行的开头?我对 RegEx 感到满意(我正在使用 /2021-06-26T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z/,因为我专门匹配 2021-06-26 的内容),只是不确定将 RegEx 匹配添加到它所在的行之前的聪明方法。
例如:
hello 2021-06-26T20:45:20.111Z
hello 2021-06-26T20:45:20.111Z hi
hi 2021-06-26T20:35:20.111Z yes
hola yes yes 2021-06-27T20:45:20.111Z
hello 2021-06-26T22:45:20.111Z
hey 2021-06-26T23:45:20.111Z
yo 2021-06-26T20:45:20.111Z no
salut 2021-06-26T20:45:20.111Z random words here
bonjour 2021-06-26T20:45:20.111Z
有没有办法将这些时间戳复制到每行的开头?例如
2021-06-26T20:45:20.111Z hello 2021-06-26T20:45:20.111Z
2021-06-26T20:45:20.111Z hello 2021-06-26T20:45:20.111Z hi
2021-06-26T20:35:20.111Z hi 2021-06-26T20:35:20.111Z yes
2021-06-27T20:45:20.111Z hola yes yes 2021-06-27T20:45:20.111Z
2021-06-26T22:45:20.111Z hello 2021-06-26T22:45:20.111Z
2021-06-26T23:45:20.111Z hey 2021-06-26T23:45:20.111Z
2021-06-26T20:45:20.111Z yo 2021-06-26T20:45:20.111Z no
2021-06-26T20:45:20.111Z salut 2021-06-26T20:45:20.111Z random words here
2021-06-26T20:45:20.111Z bonjour 2021-06-26T20:45:20.111Z
编辑:
谷歌搜索让我相信 & 代表 sed 匹配。此后的答案教会了我关于 \# 将编号组与 () 匹配
我尝试失败的事情:
sed -E '/[2021-06-26T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z/' ~/sedtest
sed '/2021-06-26T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z/' ~/sedtest
sed -E '/2021-06-26T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z/' ~/sedtest
sed -E -n '/2021-06-26T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z/' ~/sedtest
【问题讨论】: