使用加号前置文件名

时间:2012-03-15 13:59:14

标签: regex sed substitution

我想使用+在一个巨大的文件中,在所有图像名称出现之前添加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.","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,Shown with a 1.00 carat princess cut diamond.,/H22.jpg,Shown with a 1.00 carat princess cut diamond.,,,,,,Enabled,Taxable Goods,Not Visible Individually,0,0,No,Engagement Rings/Henri Daussi;;Designers/Henri Daussi,No,"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文件)。所以我需要用,[any character except dot][dot](.jpg|.gif|.png)替换,+[image_name].extension

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

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

但它不起作用。

3 个答案:

答案 0 :(得分:15)

试试这个:

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.

答案 1 :(得分:2)

's/,\([a-zA-Z0-9\/_]\+\)\(\.jpg\|\.png\|\.gif\)/,+\1\2/g'
  1. 您想要匹配的/需要转义。
  2. |需要转义。
  3. sed需要很多逃脱。

答案 2 :(得分:1)

这可能对您有用:

sed 's/\(^\|,\)\([^,.]*\.\(jpg\|png\|gif\)\)\>/\1+\2/g' file