Skip to content

Handy shell commands

Will Roper edited this page Feb 28, 2020 · 7 revisions

Run command piping output to file

./manage.py teardown --all && ./manage.py import_someplace 2>&1 | tee warning.txt

Search misc fixes

git grep --line-number -C 5 X01000000 1977a6 890908 972106 -- polling_stations/apps/data_collection/management/commands/misc_fixes.py

Find Stations with Null location and output markdown for Trello.

psql --no-align -d polling_stations -U dc -c 'SELECT '"'"'  - '"'"' || internal_council_id || chr(10) || '"'"'> '"'"' || address || chr(10) ||  postcode || chr(10) || chr(10) FROM public.pollingstations_pollingstation where "location" is null;'

Count suggestion warnings

grep SUGGESTION warning.txt | cut -c 12- | wc -l

Copy Suggestion warnings

grep SUGGESTION warning.txt | cut -c 12- | xclip -sel clip

Check all the 'suggestions' for replacing council postcodes with address base ones based on UPRNs

grep SUGGESTION warning.txt | cut -d '#' -f 2 | cut -d ':' -f 1 | sort | uniq | awk '{ print "firefox --new-window 127.0.0.1:8000/postcode/"$1" && sleep 0.5 && firefox --new-tab 127.0.0.1:8000/postcode/"$3 }'

This will open lots of windows if you have lots of suggestions...

Recalculate address to station lines

psql -d polling_stations -U dc -c 'REFRESH MATERIALIZED VIEW pollingstations_resadd_pollsta;'

Check whether UPRNs in warnings are already in your script

Useful when you have commented out a lot of previously checked uprns and want to know if they're still doing anything.

grep -Fnf <(grep -o '"[0-9]*"' warning.txt) polling_stations/apps/data_collection/management/commands/import_name_of_la.py

Merge file1.csv with file2.csv

tail -n +2 file2.csv > f2.csv
cat file1.csv f2.csv > merged.csv

Reproject coordinates

$ echo -0.1670816 51.1069636 | cs2cs -f "%.0f" +init=epsg:4326 +to +init=epsg:27700
528415	135782 0

$ echo 528414.82 135782.35 | cs2cs -I -f "%.6f" +init=epsg:4326 +to +init=epsg:27700
-0.167082	51.106964 0.000000