Skip to content

Instantly share code, notes, and snippets.

@littlehaker
Created September 23, 2013 07:41
Show Gist options
  • Save littlehaker/6667527 to your computer and use it in GitHub Desktop.
Save littlehaker/6667527 to your computer and use it in GitHub Desktop.
去除UTF8文件开头的BOM信息
#!/usr/bin/env ruby -w
# -*- coding: UTF-8 -*-
# 去除UTF8文件开头的BOM信息
# usage: cat file | ruby no-UTF8-BOM.rb
# or: ruby no-UTF8-BOM.rb file
content = ''
if ARGV.length > 0 then
# 参数模式
File.open(ARGV[0], "r") do |file|
content = file.read.force_encoding("UTF-8")
end
else
# 管道模式
content = STDIN.read.force_encoding("UTF-8")
end
puts content.gsub!("\xEF\xBB\xBF".force_encoding("UTF-8"), '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment