The Pug Automatic

Ruby script to batch find-and-replace in LiveJournal entries

Written September 11, 2007. Tagged Ruby, LiveJournal.

I got a request for a script like my Ruby script to make LiveJournal entries private in batch, but to replace a string in all entries (e.g. if you've changed the URL of your web site).

I usually do not do commission work for free, but I dislike the idea of someone having to spend ages on something I could do in a few minutes, and I had those minutes to spare. So voilà, lj_batch_replace.rb:

#!/usr/bin/env ruby
# By Henrik Nyh 2007-09-11.
# Free to modify and redistribute with due credit.

# Change these:

USERNAME = 'frank'
PASSWORD = 'sesame'


# Leave these alone unless you know what you're doing:

require 'rubygems'
require 'livejournal/login'
require 'livejournal/entry'

# So the livejournal gem doesn't choke:
# /2007/04/ruby-livejournal-batch-private#comments
LiveJournal::Entry::KNOWN_EXTRA_PROPS << "used_rte"

puts "Logging in..."
user =, PASSWORD)
login =
puts "Login response:"

puts "Getting entries..."
entries =, :recent => -1).run.values
entries = entries.sort_by { |e| e.time }

puts "Processing entries..."
entries.each do |e|
puts "#{e.time} #{e.subject}"
e.event = e.event.gsub(REPLACE_FROM, REPLACE_UNTO), e).run
puts e.event

puts "All done."

As with the batch private script, you need to install a Ruby interpreter, install RubyGems, install the LiveJournal gem (e.g. sudo gem install livejournal), configure the values at the top of the script and then run it (e.g. ruby lj_batch_replace.rb).