Skip to content

Instantly share code, notes, and snippets.

@sstephenson
Created September 25, 2011 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sstephenson/a95fd5e818f9237a1c9f to your computer and use it in GitHub Desktop.
Save sstephenson/a95fd5e818f9237a1c9f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
hexify() {
xxd -c 1 -p | sed 's/^00$/,/' | tr ',\n' '\n '
}
search="/Users/sam/.rbenv/versions/1.9.3-dev"
replace="/usr/local"
search_hex="$(echo -n "$search" | hexify)"
replace_hex="$(echo -n "$replace" | hexify)"
hexify < "$1" |
awk '
BEGIN {
search_hex=" '"$search_hex"'";
replace_hex=" '"$replace_hex"'";
split(search_hex, search);
split(replace_hex, replace);
search_len=length(search);
replace_len=length(replace);
}
{ matched=0 }
/ '"$search_hex"'/ {
result=replace_hex substr($0, length(search_hex) + 1);
for (i=length(replace)+1; i<=length(search); i++)
result=result "00 ";
print result;
matched=1
}
{ if (matched == 0) print $0 }
' |
sed 's/$/00/' |
xxd -p -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment