- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2011-08-11 14:16 编辑
高人在哪呢?
hfvbc 发表于 2011-08-11 14:04 ![]()
不完善,还能行....
$ echo '1,"nihao,shia,"shi","hiscihe",3,8
1,"nihao,sh ma",shi","hiscihe",3,8' | awk -v K='^|^' -v D="," 'function Err(e){print e" <--Error";next}{L=length($0);s=0;for(n=0;n++<L {w=substr($0,n,1);if(s==0){if(w~/[0-9]/){printf w;s=1}else if(w=="\"" {s=2}else Err(w)}else if(s==1){if(w~/[0-9]/){printf w}else if(w==D){s=0;printf K}else Err(w)}else if(s==2){if(w=="\""&&substr($0,n+1,1)==D){x=substr($0,n+2,1);if(x~/[0-9]/){s=0;printf K;n++}else if(x=="\"" {printf K;n+=2}else printf w}else printf w}}print ""}'
1^|^nihao,shia,"shi^|^hiscihe^|^3^|^8
1^|^nihao,sh ma",shi^|^hiscihe^|^3^|^8
不完善,还能行....
说明:
s=0, for initial or new item
s=1, for number
s=2, for "string"
- 1,"nihao,shia,"shi","hiscihe",3,8
- 102222222222222222202222222220101 <--- s
- 1,"nihao,sh ma",shi","hiscihe",3,8
- 1022222222222222222202222222220101 <--- s
复制代码 |
|