Skip to content

Instantly share code, notes, and snippets.

@ymkjp
Created May 28, 2015 08:02
Show Gist options
  • Save ymkjp/6a9866dba1880008a642 to your computer and use it in GitHub Desktop.
Save ymkjp/6a9866dba1880008a642 to your computer and use it in GitHub Desktop.
Multiline editing instead of painful sed or awk
# encoding: utf-8
# Usage:
# SRC_FILE="nginx.conf" DIST_FILE="nginx.conf~" ruby rough_sed.rb
from =<<EOS
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
EOS
to =<<EOS
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
EOS
string = File.read(ENV["SRC_FILE"], encoding: Encoding::UTF_8)
result = string.sub(from, to)
File.write(ENV["DIST_FILE"], result)
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment