我正在尝试删除 csv 文件中第 31 列的第一个值。这些列用引号引起来并用逗号分隔,如下所示。
"first_name","last_name","home_address"
"Frank",Russel","123 Hampton road"
第31列是图像列,图像之间用逗号分隔
"http://ddd.com/124dfr3f.jpg,http://ddd.com/hjasgx37trg.jpg"
必须从第 31 列中删除第一个图像和尾部逗号
添加- 这是一个例子。
"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","three","four","five","six","seven","eight","nine","ten","twenty-one","twenty-two","three","four","five","six","seven","eight","nine","ten","thirty-one","thirty-two","three","four","five","six","seven","eight","nine","forty"
"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","three","four","five","six","seven","eight","nine","ten","twenty-one","twenty-two","three","four","five","six","seven","eight","nine","http://pictures.domain.com/e/josequervo/1183/eeeffcc7927sssssssssss5d26c0eb19e1dc53548x.jpg","http://pictures.domain.com/e/josequervo/0083/eeef8cc7927bf1245d26c0eb19e1dc53545x.jpg,http://pictures.domain.com/e/josequervo/0099/eeefscc7927bf1245d26c0eb19e1dc53546x.jpg,http://pictures.domain.com/e/josequervo/0012/eeefdcc7927bf1245d26c0eb19e1dc53547x.jpg,http://pictures.domain.com/e/josequervo/1183/eeeffcc7927bf1245d26c0eb19e1dc53548x.jpg,http://pictures.domain.com/e/josequervo/1234/eeefgcc7927bf1245d26c0eb19e1dc53549x.jpg,http://pictures.domain.com/e/josequervo/asee/eeefhcc7927bf1245d26c0eb19e1dc53540x.jpg,http://pictures.domain.com/e/josequervo/2341/eeefjcc7927bf1245d26c0eb19e1dc53544x.jpg","two",three","four","five","six","seven","eight","nine","forty"
必须删除此网址和逗号
http://pictures.domain.com/e/josequervo/0083/eeef8cc7927bf1245d26c0eb19e1dc53545x.jpg,
我只发布了 csv 文件的前 2 行,但该文件有 300 - 400 行,并且必须从第 2 行到最后一行删除此图像。我们无法更改第一行。
答案1
使用 GNUsed
我们可以如图所示解决这个问题:
sed -e '
s/","/&\n/30
s/\n[^,]*,//
' file.csv
可以理解为:
","
在当前行中第 30 次出现的字符串处放置一个占位符。然后,从占位符一直到第一个逗号,我们删除沿途的所有内容。
另一种方法给出如下:
perl -F'/","/' -lane 'my $i;
print join q[","], map { $i++ == 30 ? substr($_, 1+index($_, ",")) : $_ } @F;
' input-file.csv
方法:
perl 将逐行读取 csv 文件,并且对于每一行,它将围绕字符串拆分
","
并将拆分元素存储在数组 @F 中。然后,对于第 31 个元素,我们删除第一个逗号之前的所有内容。
答案2
根据您的问题,图像存在于以逗号分隔的第 31 列中,我们需要从第 31 列中删除第一个字符
awk -F "," '{$31=substr($31,2);print $0}' file.csv
经过测试且工作正常,如有任何疑问请告知我们