在文件名前加上加号 [英] Prepend file names with plus sign

查看:217
本文介绍了在文件名前加上加号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个大文件中所有出现的图像名称之前添加一个 +(加号),使用 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 钻石光环订婚戒指 - 14K 白金样式:Princess-1.00ct-G-SI1 或 H-VS2-EGL-宽度:mm-尺寸:3.5,"Henri Daussi 订婚戒指与手工搭配的侧钻,镶嵌在美丽的光环中,总重 1.40 克拉.左图展示了这枚带有 1.00 克拉公主方形切割钻石的戒指.这种镶嵌可以容纳各种形状和尺寸.请联系我们了解可能的范围任何戒指.","Henri Daussi 订婚戒指,镶有漂亮光环镶边的手工匹配侧钻,总重 1.40 克拉.左图显示了这枚镶有 1.00 克拉公主方切割钻石的戒指.这种镶座可以适应各种形状和尺寸.请就任何戒指的可能性范围与我们联系.",,,,,14K 白金,Princess-1.00ct-G-SI1 或 H-VS2-EGL,,3.5,Metal_Style_Width_Size,simple,/H22.jpg,Shown带有 1.00 克拉公主方形切割钻石.,/H_22.jpg,带有 1.00 克拉公主方形切割钻石.,/H22.jpg,带有 1.00 克拉公主方形切割钻石.,,,,,,Enabled,Taxable Goods,Not单独可见,0,0,不,订婚戒指/Henri Daussi;;设计师/Henri Daussi,不,圆形、梨形、祖母绿形切割、侯爵夫人形、椭圆形、放射状、公主形切割、心形、垫形切割、ASSCHER 切割",.45-6.00 克拉,/H22.jpg

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

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

这是我迄今为止做得最好的:

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

但它不起作用.

解决方案

试试这个:

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

说明:

s#...#...#g # 替换命令.'#' 是分隔符,'g' 是多次应用它# 每一行.\(,\) # 匹配一个逗号,并保存为'\1'[^.,]\+\.# 匹配任何字符直到'.'或 ',' 找到.\(jpg\|png\|gif\) # 匹配扩展名.\1+\2 # 替换为:逗号、加号和图像名称.

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.

解决方案

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.

这篇关于在文件名前加上加号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆