简体   繁体   English

sed命令用正则表达式替换

[英]sed command replace with regex

I have been bashing my head against the wall trying to get sed to search a file are replace a timestamp. 我一直在head头,试图让sed搜索文件替换时间戳。

example replace strings (file): 示例替换字符串(文件):

<script src="<@ofbizContentUrl>/xxx/js/v6/jsfile.js?v=201711271300</@ofbizContentUrl>"></script>

<script src="<@ofbizContentUrl>/xxx/js/v6/jsfile2.js?v=20171127245</@ofbizContentUrl>"></script>

basically I want to replace the 201711271300 in the examples with the current timestamp which i have saved in a variable date. 基本上我想将示例中的201711271300替换为我在可变日期中保存的当前时间戳。 The dates in the example differ from case to case. 示例中的日期因情况而异。

I've been trying 我一直在尝试

sudo sed -i "s/?v=.+?(?=<)/?v=${date}/g" file

to no avail, going on 2 hours now 无济于事,现在进行了2个小时

Try this one 试试这个

sed "s/?v=\([0-9]*<\)/?v=${date}</g" $file

It uses a simpler pattern: all digits until you hit a < . 它使用一种更简单的模式:所有数字,直到您按下<为止。 Replace by a data (and restore the < ). 替换为数据(并恢复< )。

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

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