微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – 使用加号前置文件名

我想在一个巨大的文件中使用sed在所有出现的图像名称之前添加一个(加号).

这是一行的示例:

DAUSSI-H22-14K White Gold-Princess-1.00ct-G-SI1orH-VS2-EGL-mm-3.5,H22,7050,5720,3/5/2012 7:34,1,henri-daussi-h22-diamond-halo-engagement-ring-14k-white-gold-width--mm-style-princess-1-00ct-g-si1-or-h-vs2-egl-size-3-5,henri-daussi-h22-diamond-halo-engagement-ring-14k-white-gold-width--mm-style-princess-1-00ct-g-si1-or-h-vs2-egl-size-3-5.html,Henri Daussi H22 Diamond Halo Engagement Ring-14K White Gold-Style:Princess-1.00ct-G-SI1 or H-VS2-EGL-Width: mm-Size:3.5,"Henri Daussi engagement ring with hand-matched side diamonds in a beautiful halo setting,totaling 1.40 carats. The image at left displays this ring with a 1.00 carat princess cut diamond. This setting can accommodate a variety of shapes and sizes. Please contact us on the range of possibilities of any ring.",14K White Gold,Princess-1.00ct-G-SI1 or H-VS2-EGL,3.5,Metal_Style_Width_Size,simple,/H22.jpg,Shown with a 1.00 carat princess cut diamond.,/H_22.jpg,Enabled,Taxable Goods,Not Visible Individually,No,Engagement Rings/Henri Daussi;;Designers/Henri Daussi,"ROUND,PEAR SHAPE,EMERALD CUT,MARQUISE,oval,RADIANT,PRINCESS CUT,HEART SHAPE,CUSHION CUT,ASSCHER CUT",.45-6.00 Carat,/H22.jpg

对于这一行,图像是/H22.jpg,我想将此字符串更改为/H22.jpg.

据我所知,所有图像都以/开头,但它是一个巨大的文件,我不能完全确定.我确定的一件事是之前有一个逗号(它是逗号分隔的.csv文件).所以我需要用[image_name] .extension替换[除dot之外的任何字符] [dot](.jpg | .gif | .png).

这是迄今为止我做过的最好的事情:

sed -ie 's/,\([a-zA-Z0-9/_]\+\)\(\.jpg|\.png|\.gif\)/,+\1\2/g' file.csv

但它不起作用.

试试这个:
sed 's#\(,\)\([^.,]\+\.\(jpg\|png\|gif\)\)#\1+\2#g' infile

说明:

s#...#...#g             # Substitute command. '#' is separator and 'g' is to apply it many times for 
                        # each line.
\(,\)                   # Match a comma,and save it as '\1'
[^.,]\+\.               # Match any characters until a '.' or ',' found.
\(jpg\|png\|gif\)       # Match extension.
\1+\2                   # Replace with: Comma,plus sign and the image name.

原文地址:https://www.jb51.cc/regex/356898.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐