如何用awk分割多行记录?

如何用awk分割多行记录?

如何通过awk分割多行记录,即以下文件:

027,027,0,3,,1,0,1,1,0,0,0,0,0,0,0,0,6,1,,1.211100,1,2015-08-03,2015-07-04,,2015-11-01,0,0,2015-11-01,1,1,,0,0,0,0,0,131,0,0,0,0,0,2015-06-01,10000000000000000000,,
136,136,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,1,1,,0.350000,1,2015-08-17,2015-07-18,,2015-11-15,0,0,2015-11-15,0,0,,0,0,0,0,0,131,0,0,0,0,0,2012-11-20,10000000000000000000,,
633,633,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,3,1,,3.125300,1,2015-08-31,2015-08-01,,2015-11-29,0,0,2015-11-29,0,0,,0,0,0,0,0,131,0,0,0,0,0,2014-12-12,10000000000000000000,,
802,802,0,0,,1,0,1,1,0,1,0,0,0,0,0,0,7,1,,0.060000,1,2015-08-05,2015-07-06,,2015-11-03,0,0,2015-08-05,1,1,,0,0,0,0,0,131,0,0,0,0,0,2014-08-10,10000000000000000000,,

所需的输出 ifRS是 a"*"应如下所示:

027,027,0,3,,1,0,1,1,0,0,0,0,0,0,0,0,6,1,,1.211100,1,2015-08-03,2015-07-04,,2015-11-01,0,0,2015-11-01,1,1,,0,0,0,0,0,131,0,0,0,0,0,2015-06-01,10000000000000000000,,
********************************************************************************************************************************************************************
136,136,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,1,1,,0.350000,1,2015-08-17,2015-07-18,,2015-11-15,0,0,2015-11-15,0,0,,0,0,0,0,0,131,0,0,0,0,0,2012-11-20,10000000000000000000,,
*******************************************************************************************************************************************************************
633,633,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,3,1,,3.125300,1,2015-08-31,2015-08-01,,2015-11-29,0,0,2015-11-29,0,0,,0,0,0,0,0,131,0,0,0,0,0,2014-12-12,10000000000000000000,,
*******************************************************************************************************************************************************************
802,802,0,0,,1,0,1,1,0,1,0,0,0,0,0,0,7,1,,0.060000,1,2015-08-05,2015-07-06,,2015-11-03,0,0,2015-08-05,1,1,,0,0,0,0,0,131,0,0,0,0,0,2014-08-10,10000000000000000000,,
*******************************************************************************************************************************************************************

我尝试使用多个代码制作{RS="*"}但没有成功。

答案1

这是一种方法:

$ v='\n*******************************************************************************************************************************************************************\n'
$ awk -v ORS="$v" '{print;}' file
027,027,0,3,,1,0,1,1,0,0,0,0,0,0,0,0,6,1,,1.211100,1,2015-08-03,2015-07-04,,2015-11-01,0,0,2015-11-01,1,1,,0,0,0,0,0,131,0,0,0,0,0,2015-06-01,10000000000000000000,,
*******************************************************************************************************************************************************************
136,136,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,1,1,,0.350000,1,2015-08-17,2015-07-18,,2015-11-15,0,0,2015-11-15,0,0,,0,0,0,0,0,131,0,0,0,0,0,2012-11-20,10000000000000000000,,
*******************************************************************************************************************************************************************
633,633,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,3,1,,3.125300,1,2015-08-31,2015-08-01,,2015-11-29,0,0,2015-11-29,0,0,,0,0,0,0,0,131,0,0,0,0,0,2014-12-12,10000000000000000000,,
*******************************************************************************************************************************************************************
802,802,0,0,,1,0,1,1,0,1,0,0,0,0,0,0,7,1,,0.060000,1,2015-08-05,2015-07-06,,2015-11-03,0,0,2015-08-05,1,1,,0,0,0,0,0,131,0,0,0,0,0,2014-08-10,10000000000000000000,,
*******************************************************************************************************************************************************************

这是另一种达到相同效果但无需显式设置 ORS 的方法:

awk '{print;} {for(i=1;i<=length($0);i++)printf "*";print"";}' file

相关内容