LoginSignup
0
0

More than 5 years have passed since last update.

crontab並べ替え(テキトー)

Last updated at Posted at 2017-07-06

時系列に並べ替えます。
自分用なので超テキトーです。あしからず。

def sort_crontab(str)
  sorted_lines = []

  str.split("\n").each do |line|
    arr = line.split(" ")
    next unless arr[0]
    next if arr[0] =~ /\#/
    min = arr[0]
    hour = arr[1]
    day = arr[2]
    month = arr[3]
    week = arr[4]
    command = arr[5..arr.size].join(' ')
    mdhi_com = [min, hour, day, month, week, command]
    sorted_lines << mdhi_com
  end

  sorted_lines.sort! do |a,b| 
    (a[1].to_i <=> b[1].to_i).nonzero? || 
    (a[0].to_i <=> b[0].to_i)
  end

  sorted_lines
end

sort_crontab(str).each { |l| puts l.join(' ') }
0
0
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
0
0