Answer the question
In order to leave comments, you need to log in
Why is the output truncated when redirecting to a file?
Linux Mint. The Ruby script outputs some information to stdout, the standard puts. But I cannot guarantee that there are no tricky characters there.
When I just run it, all the text is output to the console. When I do a redirect to a file ( > file
), part of the text is cut off, not all the text is in the file, only a part (initial).
The script completes successfully. In the case of using a pipeline ( | wc
), the same is true - there are as many lines as in file. Hooking stderr ( > file 2>&1
) does not help.
At the same time, if you write directly to the file ( fputs
) in the script, then everything is in order.
# process_direct.rb использует fputs
# process.rb использует puts
# других отличий нет
$ bundle exec ruby process_direct.rb && wc f_direct
3461 5858 87579 f_direct
$ bundle exec ruby process.rb
# визуально текст весь - конец этого вывода совпадает с концом предыдущего
$ bundle exec ruby process.rb > f_1 && wc f_1
3249 5531 82131 f_1
$ bundle exec ruby process.rb | wc
3249 5531 82131
$ bundle exec ruby process.rb > f_12 2>&1 && wc f_12
3249 5531 82131 f_12
gem install happymapper
require 'happymapper'
class IpMetaData
include HappyMapper
end
resp = "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<ip-answer>\n<ip value=\"5.61.17\"><inetnum>4.16.0 - 5.255.255</inetnum><country>RU</country><city>\xCC\xEE\xF1\xEA\xE2\xE0</city><region>\xCC\xEE\xF1\xEA\xE2\xE0</region><district>\xD6\xE5\xED\xF2\xF0\xE0\xEB\xFC\xED\xFB\xE9 \xF4\xE5\xE4\xE5\xF0\xE0\xEB\xFC\xED\xFB\xE9 \xEE\xEA\xF0\xF3\xE3</district><lat>55.755787</lat><lng>37.617634</lng></ip>\n</ip-answer>"
IpMetaData.parse(resp)
puts "SUCCESS"
Answer the question
In order to leave comments, you need to log in
Didn't ^D, which means the end of the file, accidentally get stuck in the output?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question