uetennis’s diary

プログラミングについて学んだ事やライフハックについてかきます

日報 8/14

今日やる事

  • wcコマンドの課題

勉強したこと

file_data = []
count_sum = 0
size_sum = 0
bytesize_sum = 0
connection_total = []
if ARGV.length > 0
  ARGV.each do |filename|
    file_read = File.read(filename)
    file_data << file_read.count("\n").to_s.rjust(8)
    file_data << file_read.split(" ").size.to_s.rjust(8)
    file_data << file_read.bytesize.to_s.rjust(8)
    file_data << File.basename(filename).to_s.rjust(6)
    file_data << "\n"
    count_sum += file_read.count("\n")
    size_sum += file_read.split(" ").size
    bytesize_sum += file_read.bytesize
    connection_total << count_sum.to_s.rjust(8)
    connection_total << size_sum.to_s.rjust(8)
    connection_total << bytesize_sum.to_s.rjust(8)
    connection_total << "\n"
  end
else
  puts STDIN.read
end
puts file_data.join("")
puts connection_total.join("")

# (m・_・bp) 11:56 ~/memo_vscode % ruby 6memo.rb ls.rb 7memo.rb
#      127     274    2826 ls.rb
#        3       4      417memo.rb
#      127     274    2826
#      130     278    2867
  • ファイルはrjustしなくていいんだった!
file_data = []
count_sum = 0
size_sum = 0
bytesize_sum = 0
connection_total = []
if ARGV.length > 0
  ARGV.each do |filename|
    file_read = File.read(filename)

    file_data << file_read.count("\n").to_s.rjust(8)
    file_data << file_read.split(" ").size.to_s.rjust(8)
    file_data << file_read.bytesize.to_s.rjust(8)
    file_data << " "
    file_data << File.basename(filename)
    p File.basename(filename).class
    file_data << "\n"
    count_sum += file_read.count("\n")
    size_sum += file_read.split(" ").size
    bytesize_sum += file_read.bytesize
    connection_total << count_sum.to_s.rjust(8)
    connection_total << size_sum.to_s.rjust(8)
    connection_total << bytesize_sum.to_s.rjust(8)
    connection_total << "\n"
  end
else
  puts STDIN.read
end
puts file_data.join("")
puts connection_total.join("")

# (m・_・bp) 15:16 ~/memo_vscode % ruby memo.rb ls.rb nmemo.rb
# String
# String
#      127     274    2826 ls.rb
#       34     101     991 nmemo.rb
#      127     274    2826
#      161     375    3817
file_data = []
count_sum = 0
size_sum = 0
bytesize_sum = 0
if ARGV.length > 0
  ARGV.each do |filename|
    file_read = File.read(filename)
    file_data << file_read.count("\n").to_s.rjust(8)
    file_data << file_read.split(" ").size.to_s.rjust(8)
    file_data << file_read.bytesize.to_s.rjust(8)
    file_data << " "
    file_data << File.basename(filename)
    file_data << "\n"
    count_sum += file_read.count("\n")
    size_sum += file_read.split(" ").size
    bytesize_sum += file_read.bytesize
  end
  else
  puts STDIN.read
end
connection_total = []
connection_total << count_sum.to_s.rjust(8)
connection_total << size_sum.to_s.rjust(8)
connection_total << bytesize_sum.to_s.rjust(8)
connection_total << " total"
total = []
total << connection_total.join("")
count = []
count << file_data.join("")
puts count.push(total)

# (m・_・bp) 18:31 ~/memo_vscode % ruby memo.rb ls.rb nmemo.rb
#      127     274    2826 ls.rb
#       34     101     991 nmemo.rb
#      161     375    3817 total

【 wc 】コマンド――テキストファイルの文字数や行数を数える:Linux基本コマンドTips(62) - @IT