Saturday, July 31, 2010

Ruby or Groovy - splitting file records

I am researching what new modern scripting programming language I wish to learn. A friend also was kind to give various programming books to read, but when I downloaded them they had part of the URL in the file name.

$ head x -n 1
Learning.Ruby.pdf?AWSAccessKeyId=AXRXN26N8FNV8TW9FP02&Expires=1779949419&Signature=Ky2soSs3yVNDsG+02E0FIC0O01U=
$


So instead of resorting to standard unix shell programming, I decided to parse the file using one of these new fancy languages.

$ head rename.*
==> rename.groovy <==
#!/usr/bin/env groovy
new File("x").eachLine { line ->
println "mv " + '"' + line + '"' + " " + '"' + line.split(/\\?/)[0] + '"'
}

==> rename.rb <==
#!/usr/bin/env ruby
File.new("x").each { |line|
puts "mv \"" + line.gsub("\n",'" "') + line.split(/\?/)[0] + "\" \n"
}
$


They both return the same output that I can use to perform the renaming.

$ ./rename.groovy | head -n 1
mv "Learning.Ruby.pdf?AWSAccessKeyId=AXRXN26N8FNV8TW9FP02&Expires=1779949419&Signature=Ky2soSs3yVNDsG+02E0FIC0O01U=" "Learning.Ruby.pdf"
$


Needless to say I'm still undecided.

No comments: