Prepend file names with plus sign

OSdave picture OSdave · Mar 15, 2012 · Viewed 25.7k times · Source

I want to add a + (plus sign) before all the occurrences of image names in a huge file, using sed.

This is an example of one line:

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

For this line, the image is /H22.jpg and I want this string change to +/H22.jpg.

As far as I can tell, all images start with /, but it's a huge file, I can't be totally sure. One thing I am sure of is that there is a comma before (it's a comma separated .csv file). So I need to substitute ,[any character except dot][dot](.jpg|.gif|.png) with ,+[image_name].extension.

This is the best I've done so far:

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

but it's not working.

Answer

Birei picture Birei · Mar 15, 2012

Try this one:

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

Explanation:

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.