查找并替换为sed和regex

时间:2019-01-27 19:26:12

标签: macos sed

我想使用sed替换资产文件的版本。

我在文件上有这个

<script src="<?php echo assetsUrl ?>/ic-1548620973.js"></script>

并要使用以下命令更新时间戳:

sed -i "s/(?<=ic-)(.*)(?=.js)/$timestamp.js/" src/views/partials/foot.view.php

消息错误:

sed: 1: "src/views/partials/foot ...": unterminated substitute in regular expression

3 个答案:

答案 0 :(得分:2)

sed上的macOS不会像在Linux上所做的那样,因此您需要对其进行调整。

$ timestamp=$( date +%s )
$ sed -Ei '' 's@(ic-)(.*)(\.js)@\1'"$timestamp"'\3@g' src/views/partials/foot.view.php 

您需要包括-E选项,因为您使用的是捕获组,而不必进行后面的回顾,更不用说sed不是用于此目的的正确工具。对于macOS上的就地编辑,您必须在''选项后加上两个单引号-i才能将其保存到原始文件中。 / p>

出于可读性考虑,我将定界符从/更改为@。本质上,您具有三个捕获组,在其中提供$timestamp变量的同时,将仅包括第一个和第三个捕获组。

输出

<script src="<?php echo assetsUrl ?>/ic-1548622266.js"></script>

答案 1 :(得分:0)

鉴于您已安装ruby

ruby -i -ne 'puts $_.gsub(/(?<=ic-)\d+(?=\.js)/,Time.now.to_i.to_s)' src/views/partials/foot.view.php

我不确定,但是似乎ruby中已经预装了MacOS,对吗?

答案 2 :(得分:-1)

尝试一下:

index="abc" [inputlookup ids.csv | fields ids as id]
| bin _time span=1h | stats count by id _time
| bin _time span=1d | stats max(count) as countHour sum(count) as countDay by id _time
| bin _time as week span=1week | eventstats sum(countDay) as countWeek by id week
| bin _time as month span=1month | eventstats sum(countDay) as countMonth by id month
| stats max(count*) as max* by id