我有一个逗号分隔的CSV文件,但是由于某种原因,我们的系统在文件中的一个随机位置插入了一个新的行字符,这会导致整个文件中断。我可以得到文件中的列数。
如何在一个线性命令中使用sed和/或perl来解决这个问题?我知道awk可以解决这个问题,但这是为了学习的目的。如果使用perl,我不想使用内置的CSV函数。可以解决吗??我在这个问题上已经好几天了,似乎找不到解决办法。
格式错误输入样本(大量随机插入\n)
policyID,statecode,county,Point longitude,Some Thing Here,point_granularity
119736,FL,CLAY COUNTY,-81.711777,“Residential Lot”,1
448094,FL,CLAY COUNTY,-81.707664,“Residen
tial Lot”,3
206893,FL,CLAY COUNTY,-81.7
00455,“Residen
tial Lot”,1
333743,FL,CLAY COUNTY,-81.707703,“Residential Lot”,
3
172534,FL,CLAY COUNTY,-81.702675,“Residential Lot”,1
785275,FL,CLAY COUNTY,-81.707703,“Residential Lot”,3
995932,FL,CLAY COUNTY,-81.713882,
“Residential Lot”,1
223488,FL,CLAY COUNTY,-81.707146,“Residential Lot”,1
4335
12,FL,CLAY COUNTY,-81.704613,
“Residential Lot”,1policyID,statecode,county,Point longitude,Some Thing Here,point_granularity
119736,FL,CLAY COUNTY,-81.711777,“Residential Lot”,1
448094,FL,CLAY COUNTY,-81.707664,“Residential Lot”,3
206893,FL,CLAY COUNTY,-81.700455,“Residential Lot”,1
333743,FL,CLAY COUNTY,-81.707703,“Residential Lot”,3
172534,FL,CLAY COUNTY,-81.702675,“Residential Lot”,1
785275,FL,CLAY COUNTY,-81.707703,“Residential Lot”,3
995932,FL,CLAY COUNTY,-81.713882,“Residential Lot”,1
223488,FL,CLAY COUNTY,-81.707146,“Residential Lot”,1
433512,FL,CLAY COUNTY,-81.704613,“Residential Lot”,1发布于 2018-04-02 10:22:48
就像克萨兰达所说的,每行有6个字段,所以你可以试试这个词。
sed -E ':A;h;s/^/,/;s/((,[^,]+){6})(.*)/\3/;/./{g;N;s/\n//;bA};g' infilehttps://unix.stackexchange.com/questions/434979
复制相似问题