Ruby dos2unix 的问题

praguepp · 2013年04月11日 · 最后由 kikyous 回复于 2013年04月12日 · 3314 次阅读

文件是 windows 中创建的,带\r\n,每次上传到 linux 下都要 dos2unix 才能使用,想到了替换,但这样操作不成功,是没有回写么? File.open("#{path}/#{filename}.dat").each_line {|str| str.gsub!(/"\r\n"/,"\n")}

提供一偏方:用 notepad++ 之类的软件打开把回车改成 unix 风格,另存为后上传

偏方不能治本 (=@__@=) 哪。。

一是替换没写对,二是没回写

s = File.binread("#{path}/#{filename}.dat").gsub "\r\n", "\n"
File.open(s, 'wb'){|f| f << s }

换个好点的编辑器,设置换行模式为 unix 的就不会再产生这个了

把 git 安装的 bin 目录放到 path 里就 ok,搞那么麻烦

我 一般都是用 vim 打开,然后:set fileformat=unix

这种事情一般都是让编辑器替你干的 话说怎么会有 Windows 上创建的文件啊

我代码是在 windows 服务器上创建的,ftp 上传到的 linux 服务器,2 台不同的服务器传输

8 楼 已删除

#3 楼 @luikore 哈哈,还真没注意那个替换写错了

#3 楼 @luikore #9 楼 @fsword test_create_authinfo_log(TestAreaCenterClientOperation): NoMethodError: undefined method `binread' for File:Class 我用的是 1.87

:set ff=unix

#11 楼 @kikyous 在创建文件的时候加参数? 能否举个例子,谢谢哈

#11 楼 @kikyous 我是在 windows 下创建的文件

如果你是 vim 的用户的话,这个很轻松就搞定了

#10 楼 @praguepp 那就用 File.read, 写的时候把 'wb' 改回 'w'

楼主需要一个先进点的编辑器。

直接把 git config 设置 autocrlf = input

#15 楼 @luikore s = File.read("#{path}/#{filename}.dat").gsub(/"\r\n"/,"\n") File.open(s,"w"){|f| f << s }

1) Error: test_create_authinfo_log(TestAreaCenterClientOperation): Errno::EINVAL: Invalid argument - 491899858 00:05:00 1 2 2 90000 |21:1| 2 3158984320 00:05:00 1 2 2 90000 |21:1| 2 2 85000 |10000:5| 3508624407 00:05:00 1 2 2 90000 |21:1| 2 2 85000 |10000:5|

C:/operator/keywords/keyword/area_center/area_center_client_operation.rb:60:in initialize' C:/operator/keywords/keyword/area_center/area_center_client_operation.rb:60:inopen' C:/operator/keywords/keyword/area_center/area_center_client_operation.rb:60:in create_authinfo_log' C:/operator/keywords/keyword/area_center/area_center_client_operation.rb:50:intimes' C:/operator/keywords/keyword/area_center/area_center_client_operation.rb:50:in create_authinfo_log' C:/ATT_rake_server_ruby187/ruby/lib/ruby/1.8/delegate.rb:159:insend' C:/ATT_rake_server_ruby187/ruby/lib/ruby/1.8/delegate.rb:159:in method_missing' C:/ATT_rake_server_ruby187/ruby/lib/ruby/gems/1.8/gems/att-1.1.0/lib/att/keyword_manager.rb/../../att/t test_area_center_client_operation.rb:16:intest_create_authinfo_log'

#18 楼 @praguepp

不好意思写错了... 把文件的内容放到文件名那里了...

full_path = "#{path}/#{filename}.dat"
s = File.binread(full_path).gsub "\r\n", "\n"
File.open(full_path, 'wb'){|f| f << s }

P.S. 另外仔细看啊,是 gsub "\r\n", "\n" 不是 gsub /"\r\n"/, "\n"

#12 楼 @praguepp 不是用 vim 打开执行这个命令然后保存

需要 登录 后方可回复, 如果你还没有账号请 注册新账号