Skip to content

Instantly share code, notes, and snippets.

View peterdalle's full-sized avatar

Peter M. Dahlgren peterdalle

View GitHub Profile
@peterdalle
peterdalle / convertdocx2text.bat
Created May 10, 2016 08:05
Batch file to convert all DOCX files in the current directory to TXT files using Pandoc.
@echo off
REM Batch file to convert all DOCX files in the current directory to TXT files using Pandoc.
for %%f in (*.docx) do (
echo %%~nf
pandoc -f docx -t plain -o "%%~nf.txt" "%%~nf.docx"
)
@peterdalle
peterdalle / download.sh
Created March 24, 2016 14:29
Automatically download files via SSH without password
#!/usr/bin/bash
# Download CSV files from Linux server (iny my case, Raspberry Pi and Ubuntu)
# over SSH without the need to supply password. Note that Cygwin have to be
# used with this file since Git Bash uses other keyfiles.
#
# 1.Upload keyfiles:
# scp /home/username/.ssh/id_rsa.pub [email protected]:~/.ssh/authorized_keys2
# 2. Don't forget to make file executable:
# chmod +x download.sh
@peterdalle
peterdalle / CreateNewLibraryFolder.r
Created November 26, 2015 12:31
Install R libraries to the C:\Users\<You>\R\win-library\x.y\ folder in case of error message "warning in install.packages lib='...' is not writable"
dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE)
# http://stackoverflow.com/questions/5059692/unable-to-update-r-packages-in-default-library-on-windows-7
@peterdalle
peterdalle / RemoveLinesInFile.py
Created November 18, 2015 14:25
Remove lines in file A that are present in file B
#!/usr/bin/python
# Delete all lines in file A (keep.txt) that are present in file B (delete.txt).
# Rules:
# 1. If line is found in B but not in A, then delete (i.e., do not merge files).
# 2. If line is found in B and in A too, then delete (i.e., delete duplicates).
# Read file with lines that we shall keep.
keepfile = open("keep.txt")
keeplines = keepfile.readlines()
@peterdalle
peterdalle / ConvertToUTF8Collation.sh
Created August 21, 2015 18:36
Convert MySQL tables to UTF-8 collation in Bash
mysql --database=dbname -B -N -e "SHOW TABLES" \
| awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; SET foreign_key_checks = 1; "}' \
| mysql --database=dbname &
# http://stackoverflow.com/questions/105572/a-script-to-change-all-tables-and-fields-to-the-utf-8-bin-collation-in-mysql
@peterdalle
peterdalle / SverigesRiksdag.r
Last active August 29, 2015 14:27
Hämta namn, parti, valkrets och plats för alla 349 ledamöter i Sveriges riksdag
library(rvest) # install.packages("rvest")
library(plyr) # install.packages("plyr")
# Get html from url: Sveriges riksdagsledamöter 2014-2018.
riksdag.html <- html("https://sv.wikipedia.org/wiki/Lista_%C3%B6ver_ledam%C3%B6ter_av_Sveriges_riksdag_2014%E2%80%932018", encoding = "UTF8")
# Import table from html.
riksdag.tables <- html_table(riksdag.html, fill=TRUE, trim=TRUE)
riksdag.df <- riksdag.tables[[1]]
@peterdalle
peterdalle / ShowInstalledPackages.r
Created July 21, 2015 17:59
List installed packages and versions
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
print(ip, row.names=FALSE)
# From http://www.r-bloggers.com/list-of-user-installed-r-packages-and-their-versions/
@peterdalle
peterdalle / database-size.sql
Last active June 8, 2019 13:44
Get current MySQL database size in MB
-- Group by database (MB).
SELECT table_schema 'Database Name',
SUM(data_length + index_length ) / 1048576 'Database Size (MB)',
SUM(data_free) / 1048576 'Free Space (MB)'
FROM information_schema.TABLES
GROUP BY table_schema;
-- Group by database and table (bytes).
SELECT table_schema 'databasename',
table_name 'tablename',
@peterdalle
peterdalle / bensinpris.py
Last active August 29, 2015 14:24
Hämta det billigaste bensinpriset i Göteborg
# Visa det billigaste bensinpriset i Göteborg, via bensinpriser.se.
import requests # pip install requests
from bs4 import BeautifulSoup # pip install BeautifulSoup4
r = requests.get('http://www.bensinpriser.se/v%C3%A4stra-g%C3%B6talands-l%C3%A4n/g%C3%B6teborg/g%C3%B6teborg')
soup = BeautifulSoup(r.text, "lxml") # pip install lxml
rowIndex = 0
for row in soup.select(".resulttable tr"):
rowIndex = rowIndex + 1
@peterdalle
peterdalle / CreateBuildDate.bat
Last active August 29, 2015 14:20
Create property for build date in VB.NET
@echo off
echo Build date %DATE%
REM Write to file: BuildDate.vb
echo ' This file is autogenerated by CreateBuildDate.bat. Changes here will be lost. > BuildDate.vb
echo Namespace Snuffbox.Configuration >> BuildDate.vb
echo Public Partial Class Settings >> BuildDate.vb
echo Public Shared ReadOnly Property BuildDate As String >> BuildDate.vb
echo Get >> BuildDate.vb
echo Return "%DATE% %TIME:~0,8%" >> BuildDate.vb