利用桑达以取代旧的时间戳和当前 [英] Using Sed to replace old timestamp with current one

查看:117
本文介绍了利用桑达以取代旧的时间戳和当前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用桑达找内的template.php文件中一个JS文件的名称的Unix时间戳部分和当前时间戳替换它。旧的时间戳会有所不同,所以我需要一个正则表达式匹配一个数字。

I'd like to use Sed to find within a template.php file the unix timestamp portion of the name of a JS file and replace it with a current timestamp. The old timestamp will vary so I need a regex to match a number.

下面是我到目前为止有:

Here's what I have so far:

TimPeterson$ current_timestamp=date +%s
TimPeterson$ sed -i "" "s/myJS-[0-9]*\([0-9]\+\).js/myJS-$current_timestamp.js/" template.php

只是要对问题清楚,我需要的JS文件名匹配看起来是这样的(其中的数字部分是可变的):

Just to be clear on the problem, what I need to match in the JS file name looks something like this (where the number part is variable):

myJS-1361297970.js

然而, [0-9] * \\([0-9] \\ + \\)是不正确的。

推荐答案

在code,因为它代表的几个原因无法正常工作。与比较吧:

The code as it stands does not work for several reasons. Compare it with:

current_timestamp=`date +%s`
sed -ie "s/myJS-[0-9]\+\.js/myJS-${current_timestamp}.js/g" template.php

评论:


  1. 日期+%S 有在里面后引号,以获得执行程序中的 CURRENT_TIMESTAMP 变量

  2. 模式 [0-9] \\ + 就足以捕获所有的名字和一系列数字。

  3. 我加括号来分隔变量名称

  4. 保护的.js 部分不会与元字符混淆点。,任何字符相匹配但回车。

  5. 我加了一个G修改器替代,产生一些替代在同一行。

  1. date +%s has to be inside backquotes to get the output of the execution of the program into the current_timestamp variable
  2. The pattern [0-9]\+ is enough to capture all the names with a series of digits.
  3. I added braces to delimit variable name
  4. Protected the dot of the .js part not to be confused with the metacharacter ., that matches any character but carriage return.
  5. I added a "g" modifier to the substitution to produce several substitution in the same line.

这篇关于利用桑达以取代旧的时间戳和当前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆