Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepend file names with plus sign

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.

like image 424
OSdave Avatar asked Mar 15 '12 13:03

OSdave


People also ask

What is ~$ in a file name?

From Wikipedia: “The tilde symbol is used to prefix hidden temporary files that are created when a document is opened in Windows. For example, when you open a Word document called “Document1. doc,” a file called “~$cument1.

Can you use symbols in file names?

Valid Special Characters for File and Folder NamesSpecial characters are symbols such as & (ampersand) or * (asterisk). Some require a shift key stroke, such as the special characters grouped in the number keys, while others do not, such as / (forward slash).

Why there is * At the end of file name?

It means that the file is executable.


1 Answers

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.
like image 50
Birei Avatar answered Nov 08 '22 03:11

Birei