Last active
December 15, 2017 13:44
-
-
Save kou1okada/459da6a2134cd26eb21d to your computer and use it in GitHub Desktop.
ren.rb - rename by the regular expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# ren.rb - rename by the regular expressions | |
# Copyright (c) 2016 Koichi OKADA. All rights reserved. | |
# This script is distributed under the MIT license. | |
# | |
require 'optparse' | |
$config = { | |
:i => "", | |
} | |
pat = /./ | |
rep = '\0' | |
opts = OptionParser.new | |
opts.banner = "Usage: #{File.basename $0} [options ...] filename ..." | |
opts.banner << <<'END' | |
This script only output rename operations with mv command. | |
PATTERN can take the regular expressions. | |
REPLACE can take special escape sequences. | |
\&, \0 is the matched string | |
\1 ... \9 is the n th matched group | |
\`, \' is pre and post string of matched | |
\+ is last matched group | |
options: | |
END | |
opts.on("-p PATTERN") {|v| pat = Regexp.new v} | |
opts.on("-r REPLACE") {|v| rep = v} | |
opts.on("-i", "interactive") {|v| $config[:i] = "-i "} | |
opts.parse! | |
ARGV.each do |v| | |
if pat =~ v | |
puts "mv -v -- #{$config[:i]}\"#{v.gsub(/"/,'\"')}\" \"#{v.gsub(pat, rep).gsub(/"/,'\"')}\"" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment