Skip to content

Instantly share code, notes, and snippets.

@ejhayes
ejhayes / read_from_file_or_pipe_and_perform_regex_replacement.sh
Last active December 25, 2015 00:39
Colorizes output, reads from file or stdin, runs sed replacement on incoming input.
#!/bin/bash
set -e
function warn {
# Yellow'ish
echo -e "\033[1;33m$1\033[0m" 1>&2
}
function info {
@ejhayes
ejhayes / regex_replace_text_with_sed.sh
Created October 7, 2013 23:53
Perform string replacement on a file using regex and sed. Boo yah
sed -i "s/THING_TO_REPLACE/REPLACE_WITH_THIS/g" path/to/file
@ejhayes
ejhayes / capture_protocol_and_host_http.regex
Created September 27, 2013 22:07
Capturing protocol and host in regex, with non-capturing group. I always seem to need this and don't want to keep writing this out.
^(?:(?:(https?):)?\/\/)?([\w\.]+)\/?
@ejhayes
ejhayes / address_extraction_regex.rb
Created June 13, 2013 18:14
Regex with optional groups. Useful for pulling out information about what you are looking for and where you are looking for it. For example: police records near sacramento, ca
# Uses this regex: ^((?:(?!(?:near|in)).)*)(?:(?:near|in)\s*(.*))?$
irb(main):001:0> /^((?:(?!(?:near|in)).)*)(?:(?:near|in)\s*(.*))?$/.match('police records near sacramento, ca')
=> #<MatchData "police records near sacramento, ca" 1:"police records " 2:"sacramento, ca">
irb(main):002:0> r=/^((?:(?!(?:near|in)).)*)(?:(?:near|in)\s*(.*))?$/.match('police records near sacramento, ca')
=> #<MatchData "police records near sacramento, ca" 1:"police records " 2:"sacramento, ca">
irb(main):003:0> r[0]
=> "police records near sacramento, ca"
irb(main):004:0> r[1]
=> "police records "
@ejhayes
ejhayes / Gemfile
Last active December 17, 2015 23:09
Enable cucumber interactive debugging. Took me a while to find current documentation for this (or I was just looking in the wrong place). All you need to do is include the new debugger gem in your gemfile, add a debugging step definition, then update your features.
group :test, :development do
...
gem "rspec-rails", "~> 2.0"
gem "cucumber-rails", :require => false
gem "capybara"
gem "debugger"
...
end
@ejhayes
ejhayes / rollback_past_git_commits.sh
Created May 30, 2013 22:56
Git commit rollback. I have a repository that has some previous commits that I now want to roll back. My commit history looks like this: A -> B -> C -> D -> E -> F -> G I just committed to G, but realized that commits C and D should never have been made. How can I roll this back?
#!/bin/bash
$ git log --oneline
066087b Adding new support.
3f21325 removing the test file
a4cde53 updating a file again
a86af8a updating a file
cdec8c0 testing a git commit
c25db10 added assets
2554c74 made header text a link
@ejhayes
ejhayes / backup_partial_table.sh
Created May 30, 2013 00:31
Export part of a table from MySQL. Someone can edit this file, send it back, then we can just overwrite the existing rows. I am doing this for a legacy database that has confusing logic to retrieve this information. Updating the rows seems to be the safest way to update this data without impacting other site functionality.
#!/bin/bash
set -e
die () {
echo >&2 "$@"
echo >&2
echo >&2 "USAGE: $0 [database]"
exit 1
}
@ejhayes
ejhayes / get_sql_file_contents_and_sample_data.sh
Created May 29, 2013 22:54
Iterates over sql files, imports them into a dummy db, gets information about table structure, prints out 3 sample rows, drops the table.
#!/bin/bash
for DB_FILE in $(ls *.sql); do
echo "Using $DB_FILE"
head -c $(expr length "Using $DB_FILE") < /dev/zero | tr '\0' '='
echo
mysql erictest2 < $DB_FILE
mysql erictest2 --batch --silent -e 'show tables' | xargs -I tbl bash -c "echo Table structure for \'tbl\' && mysql erictest2 -t -e 'desc tbl' && mysql erictest2 -t -e 'select * from tbl limit 3\G; drop table tbl;'"
echo
done > out.txt
-- Locations
create table locations (
id int not null auto_increment primary key,
feature_id int not null,
state_id int not null,
place_id int not null,
type varchar(255) not null,
name varchar(255) not null,
county_name varchar(255),
latlng point NOT NULL,
@ejhayes
ejhayes / generate_patch_files_and_apply_them.sh
Created May 9, 2013 23:39
Using git to generate patch files and patch to apply them.
# Take the last 2 commits from git and create a patch file
$ git format-patch -2 HEAD --stdout > 0001-last-2-commits.patch
# Doing a dry run of a patch
$ patch --dry-run -i 0001-last-2-commits.patch -p1
patching file ...
# Actually applying the patch file
$ patch -i 0001-last-2-commits.patch -p1
patching file ...