日期:2014-05-16  浏览次数:20944 次

Linux Sed 命令替换windows下面的回车换行符
CSS code


现有一批CSV数据,第一列URL中有一个回车换行符变成了两行,导致用SQLLOAD导入数据库就变成了两条记录!

我想用SED 命令把那条记录替换成正常的不换行的数据!
我试过很多命令都不行,请大神们赐教!
我试过的命令有:

# sed  -r ':a;s/\n//g;' 9.csv >> test.csv
# sed  -r ':a;s/\r\n//g;' 9.csv >> test.csv
# sed  -r ':a;s/\r//g;' 9.csv >> test.csv
# sed  -r ':a;N;s/\n//g;' 9.csv >> test.csv
# sed  -r ':a;N;s/^M//g;' 9.csv >> test.csv
# sed  -r ':a;N;s/$//g;' 9.csv >> test.csv
# sed  -r ':a;N;s/*$//g;' 9.csv >> test.csv
等等一些命令的尝试都不成功,有没有大神想出更好的方法,能否告之小弟,在此感激不尽!!












"www.ibm.com/cn/zhacb",4/1/2012,7554,"0.0%",5629,"0.0%",5347,"0.1%",3266,"0.0%",2165,"0.1%","30.7%",32,2488,"0.1%",3076,"0.0%"
"www.ibm.com/cn/[color=#FF0000]zh[/color]",4/4/2012,7287,"0.0%",5202,"0.0%",4943,"0.0%",3037,"0.0%",1984,"0.1%","29.6%",35,2116,"0.0%",2876,"0.0%"
"www.ibm.com/cn/zhdsd",4/7/2012,5672,"0.0%",4098,"0.0%",3879,"0.0%",2605,"0.0%",1361,"0.0%","32.7%",34,1819,"0.0%",2168,"0.0%"
"www.ibm.com/cn dcc, \n<br/> zh
",4/2/2012,5474,"0.0%",4001,"0.0%",3840,"0.0%",2654,"0.0%",1248,"0.0%","36.8%",32,1874,"0.0%",2011,"0.0%"





------解决方案--------------------
按现有的文件内容来考虑,末尾不是双引号的则把下一行并上来。如果还有其它的格式可能要再调整:
C/C++ code
[nicenight@CSDN ~]$ cat file.txt 
"www.ibm.com/cn/zhacb",4/1/2012,7554,"0.0%",5629,"0.0%",5347,"0.1%",3266,"0.0%",2165,"0.1%","30.7%",32,2488,"0.1%",3076,"0.0%"
"www.ibm.com/cn/[color=#FF0000]zh[/color]",4/4/2012,7287,"0.0%",5202,"0.0%",4943,"0.0%",3037,"0.0%",1984,"0.1%","29.6%",35,2116,"0.0%",2876,"0.0%"
"www.ibm.com/cn/zhdsd",4/7/2012,5672,"0.0%",4098,"0.0%",3879,"0.0%",2605,"0.0%",1361,"0.0%","32.7%",34,1819,"0.0%",2168,"0.0%"
"www.ibm.com/cn dcc, \n<br/> zh
",4/2/2012,5474,"0.0%",4001,"0.0%",3840,"0.0%",2654,"0.0%",1248,"0.0%","36.8%",32,1874,"0.0%",2011,"0.0%"
"www.ibm.com/cn dcc, \n<br/> zh
",4/2/2012,5474,"0.0%",4001,"0.0%",3840,"0.0%",2654,"0.0%",1248,"0.0%","36.8%",32,1874,"0.0%",2011,"0.0%"
"www.ibm.com/cn/zhdsd",4/7/2012,5672,"0.0%",4098,"0.0%",3879,"0.0%",2605,"0.0%",1361,"0.0%","32.7%",34,1819,"0.0%",2168,"0.0%"
"www.ibm.com/cn dcc, \n<br/> zh
",4/2/2012,5474,"0.0%",4001,"0.0%",3840,"0.0%",2654,"0.0%",1248,"0.0%","36.8%",32,1874,"0.0%",2011,"0.0%"
[nicenight@CSDN ~]$ sed '/[^"]$/{N;s/\n//}' file.txt 
"www.ibm.com/cn/zhacb",4/1/2012,7554,"0.0%",5629,"0.0%",5347,"0.1%",3266,"0.0%",2165,"0.1%","30.7%",32,2488,"0.1%",3076,"0.0%"
"www.ibm.com/cn/[color=#FF0000]zh[/color]",4/4/2012,7287,"0.0%",5202,"0.0%",4943,"0.0%",3037,"0.0%",1984,"0.1%","29.6%",35,2116,"0.0%",2876,"0.0%"
"www.ibm.com/cn/zhdsd",4/7/2012,5672,"0.0%",4098,"0.0%",3879,"