1
2

More than 5 years have passed since last update.

Ruby 特定行のみの出力

Posted at

ファイルの中から特定のパターンの行のみを選んで出力する

simple_grep.rb
pattern = Regexp.new(ARGV[0])
filename = ARGV[1]
file = File.open(filename)
file.each_line do |line|
  if pattern = ~line
   print line
  end 
end
file.close

ruby simple_grep.rb パターン ファイル名
ruby simple_grep.rb matz changelog

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2