简体   繁体   English

Sed用法与记住的部分

[英]Sed Usage With Remembered Portions

Lets say I have an image in the format img_12-31-06_99.jpg 可以说我有一张图片,格式为img_12-31-06_99.jpg

Now I am trying to move the '.jpg' to the front of the expression to get 现在,我正在尝试将'.jpg'移动到表达式的前面以获取

`.jpgimg_12-31-06_99`

So I tried matching the first portion of the expression with img_[0-9]*-[0-9]*-[0-9]*_[0-9]* 因此,我尝试将表达式的第一部分与img_[0-9]*-[0-9]*-[0-9]*_[0-9]*匹配

then .jpg to match the remainder. 然后.jpg匹配其余部分。 I used \\2 to move the 2nd portion to the front and \\1 to move the first expression caught to the end. 我使用\\2将第二部分移到前面,并使用\\1将捕获的第一个表达式移到末尾。

echo img_12-31-06_99.jpg | sed 's/\(img_[0-9]*-[0-9]*-[0-9]*_[0-9]*\) \(.jpg\)/\2 \1/' 

Now this returns my original expression : 现在,这将返回我的原始表达式:

img_12-31-06_99.jpg

Can anyone explain to to me please? 有人可以向我解释吗?

You are close, you have a problematic space in both the regex and the replacement, the following should work: 您亲近了,正则表达式和替换项中都有问题的空间,以下方法应该起作用:

sed 's/\(img_[0-9]*-[0-9]*-[0-9]*_[0-9]*\)\(.jpg\)/\2\1/'

For example: 例如:

$ echo img_12-31-06_99.jpg | sed 's/\(img_[0-9]*-[0-9]*-[0-9]*_[0-9]*\)\(.jpg\)/\2\1/'
.jpgimg_12-31-06_99

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

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