本帖最后由 lionfun 于 2011-08-10 20:56 编辑
回复 1# hfvbc
你的问题比较特殊啊,我通过分几种情况去匹配了。写完这个sed,连我自己都看不懂!![]() - [root@M2_lion_S10_115.238.72.66_61618_A dir]# cat file1
- 1,"nihao,shia,"shi","hiscihe",3,8
- 1,"nihao,shma",shi","hiscihe",3,8
- [root@M2_lion_S10_115.238.72.66_61618_A dir]# sed -r 's/([^",][^",]+)",([^",][^",]+)/\1-ab-\2/g;s/([^",][^",]+),"([^",][^",]+)/\1-ba-\2/g;s/([^,][^,]+),([^,][^,]+)/\1-b-\2/g;s/","|",|,"|,/^|^/g;s/-ab-/",/g;s/-ba-/,"/g;s/-b-/,/g' file1
- 1^|^nihao,shia,"shi^|^hiscihe^|^3^|^8
- 1^|^nihao,shma",shi^|^hiscihe^|^3^|^8
- [root@M2_lion_S10_115.238.72.66_61618_A dir]# cat file2
- 1,"nihao,shia,"shi","hiscihe",3,8
- 1,"nihao,shma",shi","hiscihe",3,8
- 1,"nihao,"shma",shi","hiscihe",3,8
- 1,"nihao,s"hma",shi","hiscihe",3,8
- 2,1,"nihao,shma",shi",2,5,6,"hiscihe",3,8,9
- [root@M2_lion_S10_115.238.72.66_61618_A dir]# sed -r 's/([^",][^",]+)",([^",][^",]+)/\1-ab-\2/g;s/([^",][^",]+),"([^",][^",]+)/\1-ba-\2/g;s/([^,][^,]+),([^,][^,]+)/\1-b-\2/g;s/","|",|,"|,/^|^/g;s/-ab-/",/g;s/-ba-/,"/g;s/-b-/,/g' file2
- 1^|^nihao,shia,"shi^|^hiscihe^|^3^|^8
- 1^|^nihao,shma",shi^|^hiscihe^|^3^|^8
- 1^|^nihao,"shma",shi","hiscihe^|^3^|^8
- 1^|^nihao,s"hma",shi^|^hiscihe^|^3^|^8
- 2^|^1^|^nihao,shma",shi^|^2^|^5^|^6^|^hiscihe^|^3^|^8^|^9
- [root@M2_lion_S10_115.238.72.66_61618_A dir]#
复制代码 |