Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active February 26, 2016 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou1okada/47fcb4e45d336208e3eb to your computer and use it in GitHub Desktop.
Save kou1okada/47fcb4e45d336208e3eb to your computer and use it in GitHub Desktop.
wmic2tsv.rb: Convert WMIC to TSV.
#!/bin/sh
exec ruby -lnx "$0" "$@"
#!ruby
# encoding: utf-8
#
# Copyright (c) 2016 Koichi OKADA. All right reserved.
# This script is distributed under the MIT license.
#
BEGIN {
cmd = File.basename $0
help = <<EOD
Usage: #{cmd} [FILE]
Convert WMIC to TSV.
ex)
$ # for Headerlesss UCS2 format
$ wmic cpu | iconv -f UCS2 -t UTF-8 > cpu.txt
$ #{cmd} cpu.txt
or
$ wmic cpu | iconv -f UCS2 -t UTF-8 | #{cmd}
or
$ # for WMIC header and USC2 format
$ xxd cpu.txt | head -n4
00000000: 574d 4943 2082 f083 4383 9383 5883 6781 WMIC ...C...X.g.
00000010: 5b83 8b82 b582 c482 a282 dc82 b781 4282 [.............B.
00000020: b582 ce82 e782 ad82 a891 d282 bf82 ad82 ................
00000030: be82 b382 a281 4241 0064 0064 0072 0065 ......BA.d.d.r.e
$ dd if=cpu.txt bs=1 skip=$[0x37] 2>/dev/null \\
| iconv -f UCS-2LE -t UTF-8 | wmic2tsv.rb
EOD
if (STDIN.tty? && ARGV.length <= 0)
STDERR.puts help
exit 1
end
vals =[]
}
r = []
s = $_
if (ARGF.lineno == 1)
# Read field names.
# Field names also defines field size.
while (s =~ /[^ ]* {2,}/)
r.push($&)
s = $'
end
else
# Read field value.
# Field size is defined by the width of field name.
vals[0].length.times do |i|
l = vals[0][i].length
r.push(s[0...l])
s = s[l..-1]
end
end
vals.push(r)
END {
vals.transpose.each do |r|
print r.join("\t")
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment