我正在尝试编写一个bash脚本来修改从一个MySQL数据库导出的CSV文件,以导入另一个MySQL数据库。
输入文件样本。
12345,This is a test description,This is a test priority,[1494372600,1494376200,1494546300,1494549900]
54321,This is a another test description,This is another test priority,[1494956700,1494958500]
如果重要的话,我也可以将其导出为以下内容。
12345,This is a test description,This is a test priority,1494372600,1494376200,1494546300,1494549900
54321,This is a another test description,This is another test priority,1494956700,1494958500
我的目标是让它看起来如下。
12345,This is a test description,This is a test priority,[[1494372600,1494376200],[1494546300,1494549900]]
54321,This is a another test description,This is another test priority,[[1494956700,1494958500]]
每条记录中的最后一个字段是开始时间和结束时间对。这里的关键是每条记录导出的字段数量取决于每条记录的开始时间和结束时间对的数量。任何帮助将不胜感激。
谢谢!
答案 0 :(得分:0)
awk
救援!
使用第一种格式
awk -F'[' '{n=split($2,a,","); s=FS;
for(i=1;i<n;i+=2) s=s FS a[i] "," a[i+1] "]" (i==(n-1)?"":",");
print $1 s}' file
还假设其他字段中没有方括号。
答案 1 :(得分:0)
使用GNU awk为第3个arg匹配():
$ awk 'match($0,/(.*)(\[.*)/,a){gsub(/[^,]+,[^,]+/,"[&]",a[2]); $0=a[1] a[2]} 1' file
12345,This is a test description,This is a test priority,[[1494372600,1494376200],[1494546300,1494549900]]
54321,This is a another test description,This is another test priority,[[1494956700,1494958500]]
使用其他awks,您需要添加一个变量加上substr()。