我有一个csv,但我不希望单元格值超过255个字符(在25列中,这只是第10列的问题)。
我怎么能这样做(我认为这在awk中最容易,但是随意使用sed或者任何脚本语言对你来说最容易)?
作为一个例子,这个: 1,2,3,4,5,6,7,8,9,1234567890a1234567890b1234567890c1234567890d1234567890e1234567890f1234567890g1234567890h1234567890i1234567890j1234567890k1234567890l1234567890m1234567890n1234567890o1234567890p1234567890q1234567890r1234567890s1234567890t1234567890u1234567890v1234567890w1234567890x1234567890y1234567890z1234567890,11,12,13,14,15,16,17,18
会转换为此 1,2,3,4,5,6,7,8,9,1234567890a1234567890b1234567890c1234567890d1234567890e1234567890f1234567890g1234567890h1234567890i1234567890j1234567890k1234567890l1234567890m1234567890n1234567890o1234567890p1234567890q1234567890r1234567890s1234567890t1234567890u1234567890v1234567890w12,11,12,13,14,15,16,17,18
答案 0 :(得分:3)
awk 'BEGIN{FS=OFS=","} {$10=substr($10,1,255)} 1' file
答案 1 :(得分:3)
这可能适合你(GNU sed):
sed -r 's/([^,]{0,255})[^,]*/\1/10' file
答案 2 :(得分:2)
awk -F, 'BEGIN {OFS=","} {$10=substr($10,1,255);print}'
答案 3 :(得分:0)
Perl在这里不如awk那么好:
perl -F, -ane '$,=","; $F[9] =~ s/^.{255}\K.*//; print @F' file