2015/12/30

Awk

Filter specific lines via regex,
extract specific field from filtered lines,
summarize the extracted field values
and print the sum


awk 'match($0, /Some value: (\d+)$/, ary) {sum += ary[1]} END {print sum}' lines.txt

Get current git branch fast:
alias br='git rev-parse --abbrev-ref HEAD'
alias br='awk -F/ "{print \$NF}" .git/HEAD'

2015/12/03

Bash: search two lines in a file

grep -rl 'use Module1' . | xargs -I{} grep -l 'use Module2' '{}'

ag -l 'use Module1' | xargs -I{} ag -l 'use Module2' '{}'

2015/11/27

Parsing a smushed string

If you had a string of words smushed together without spaces, how would you go about parsing the string into words again?

http://blogs.perl.org/users/ingy_dot_net/2015/11/perl-regular-expression-awesomeness.html

https://gist.github.com/ingydotnet/94528c938ca94f684270

#!/usr/bin/env perl

use strict;

use Data::Printer;

my $input = 'minusthemessageforeverytriedword';

# All 3+ letter English words, longest to shortest:
my @long = grep {length > 2}
    sort {length $b <=> length $a}
    map {chomp, $_}
    `cat /usr/share/dict/words`;

# `for` over `fore`, `the` over `them`
unshift @long, qw( the for );

# Too many small words in dict file. Use these:
my @short = qw(
    ad ah am an as at ax be by do go he hi if in is it
    me my no of oh on or ox pi so to up us we yo a I
);
# Make a gigantic list of words for the regexp:
my $list = join '|', @long, @short;

my @words = $input =~ /\G($list)(?=(?:$list)*\z)/g;

p @words;

Read from STDIN

my $input = do {local $/; <>};

2015/11/19

GPG

Import keys from a public server:
gpg --keyserver pgp.mit.edu --search-keys <email>


gpg v1

Encrypt messages #1:
echo "hello" | gpg --symmetric --armor --passphrase "asdf" > message
cat message | gpg

Encrypt messages #2:
gpg --encrypt --armor <filename>
gpg --decrypt --armor <filename>

How to GPG:
brew install gnupg
gpg --list-secret-keys
gpg --list-public-keys
gpg --gen-key


gpg v2

brew install gnupg2
echo "hello" > message
gpg -se -ar "Name Surname" message

gpg -se -ar <long key> message
echo "hello" | gpg -e -ar <long key>
gpg -d message.asc

gpg --list-keys

gpg --keyserver pgp.mit.edu --receive-keys <long key>

2015/11/03

Convert timestamp to UTC date

select CONVERT_TZ(FROM_UNIXTIME(1443737234), @@session.time_zone, '+00:00') AS utc;

Get time zone from MySQL

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

SELECT EXTRACT(HOUR FROM (TIMEDIFF(NOW(), UTC_TIMESTAMP))) AS `timezone`;

MySQL Timezone Cheatsheet

Read this

2015/11/02

Install modules via cpanm and cpanfile

Install cpanm from:
http://xrl.us/cpanm

cat cpanfile:
requires 'File::Slurper';

command to install:
cpanm --quiet --no-man-pages --notest --installdeps .

2015/10/01

Search for Perl module - INC

In ~/.bashrc (~/.bash_profile on Mac):

search () { (
    # Searches for perl module. Usage: "search my::modul"
    for DIR in $(perl -Ilib -It -e 'pop @INC if $INC[-1] eq q{.}; print join " ", @INC');
    do find $DIR -iwholename *${@//:://}*.pm; done
) };

2015/09/15

Perl and UTF-8

perl -CS -e 'print'

---

use utf8;

---

binmode STDOUT, ':encoding(UTF-8)';
binmode STDOUT, ':utf8';

---

use encoding 'utf-8';

2015/08/17

Sudo on OS X

http://blog.rongarret.info/2015/08/psa-beware-of-sudo-on-os-x.html

2015/07/03

Bash: init from arg

Initialize from argument:
TEST=$(test -n "$1" && echo $1) || { echo "Usage: $0 <path_to_file>"; exit; }

test -z $1 && echo "Usage: $0 <path_to_file>" && exit
TEST=$1

2015/06/25

Divide with floating point

awk 'BEGIN {printf "%.2f \n", (11.226 - 8.342)/11.226*100}'
awk 'BEGIN {print (11.226-8.342)/11.226*100}'

Rounding up works by default:
awk 'BEGIN {printf "%.3f \n", (11.226 - 8.342)/11.226*100}'

2015/06/12

Filter STDERR only

$ cat warn.pl
#!/usr/bin/env perl
print STDERR "warnings all over the place\n";
print STDOUT "standard output to file descriptor 1\n";

$ exec 3>&1; ./warn.pl 2>&1 >&3 3>&- | awk '{print $5}' 3>&-; exec 3>&-
standard output to file descriptor 1
place

2015/05/26

.bashrc

~/.bashrc
~/.bash_profile

export PS1="\[\033[36m\]\u\[\033[m\]@\h:\[\033[32m\]\w\[\033[m\]\$ "
export PS1="\[\033[36m\]\A \[\033[32m\]\w/ \[\033[33m\]\$ \[\033[m\]"
export PS1="\[\033[32m\]\w/ \[\033[33m\]\$ \[\033[m\]"
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'
alias l='ls -l'
alias md='mkdir -p'
alias ..='cd ..'

alias grep='grep --color=auto -n'

2015/05/13

Oracle

select now():
select to_char(sysdate, 'mm-dd-yyyy hh24:mi:ss') "now" from dual;

select last row:
select * from table where id = (select max(id) from table);

2015/05/07

Git

Current branch
git rev-parse --abbrev-ref HEAD
awk -F/ "{print \$NF}" .git/HEAD

Log
git log -1
git show 2a32e8590d5c00364b3c025f4c5a08f7aed26fd8
git log -- oneline -- abbrev - commit -- all -- graph

Show
git show f1ec7a9:lib/perl/Module.pm

Show changes history
git log -GMyChangedString -p

Show file history
    commit history
    git log -- filename
    commit history and diffs
    git log -p filename
    tig filename, then j, k, d, q

        To resolve Ctrl+Y in tig:
        stty -a | grep dsusp
        stty dsusp undef
        stty -a | grep dsusp

Rename a branch
git branch -m branch-old branch-new
git push origin :branch-old
git push --set-upstream origin branch-new

Revert a merge to master
git revert -m 1 d33e978a8be12e8023dac8aba2cc126098dfa15d
(where d33e97... is the merge commit)

Revert commit
git revert <hash>

Delete a branch
git branch -D bugfix
git push origin --delete bugfix

Revert branch to remote
git reset --hard origin/bugfix

Resolve a file (ours is HEAD, theirs is trying to merge into HEAD)
git checkout --ours file.c
git checkout --theirs file.c
git add file.c
git commit -m 'File added'

Named stashes
git stash save "guacamole sauce WIP"
git stash apply stash^{/guacamo}

Git patch
git format-patch branch-18 --stdout > 18.patch
git format-patch HEAD^ --stdout > 18.patch
git apply --stat 18.patch
git apply --check 18.patch
git apply 18.patch

Git diff
git diff --stat -p master..branch
git diff branch^..branch
git diff HEAD~1 --stat

Git bisect
git log --since=1.weeks --reverse | head -n3

git bisect start
git bisect bad
git bisect good <hash>
  git bisect run my_script arguments
git bisect reset

Git force push
To force a push to only one branch, use a + in front of the refspec to push
git push origin +branch
git push --force-with-lease origin branch

If git lock occurs
ps -ef | grep git
rm .lock
git fetch --all --prune
(cleans up old branches locally that are no longer on the git remote server)
git checkout branch
git pull

Bring back the removed file

Existing commit

get a list of removed files

git checkout HEAD
OR
git checkout --

then restore needed file

git checkout HEAD file1 file2
OR
git checkout -- file1 file2
OR
git checkout HEAD^ -- file1 file2 file3

Previous commit

git reset HEAD^ -- file1 file2
git checkout -- file1 file2

Previous commits

git reset HEAD~4 -- file1 file2
git checkout -- file1 file2

How many commits is HEAD far away from origin/master?

git rev-list --count HEAD ^origin/master

Clean git dangling objects

git fsck
git gc --prune=now

Rebasing a big big branch

http://blog.appsignal.com/blog/2016/09/27/git-rebasing-strategies.html

Advanced Git commands

https://hackernoon.com/lesser-known-git-commands-151a1918a60

Git notifications link:
https://github.com/notifications/participating

Git pull requests link:
https://github.com/pulls/mentioned

Hide Whitespace Noise:
git diff -w
git show -w
?w=1

Show Changed Words:
git diff --word-diff

Which Branches You Recently Worked On:
recent = ! git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:short)"

Remind Yourself What You’ve Been Up To:
git log --all --oneline --no-merges --author=<your email address>


Current hash:
current = ! git log -1 --pretty=oneline | cut -d' ' -f1
git rev-parse HEAD

Empty commit:
git commit --allow-empty -m "trigger build"

Lines removed so far:
git log --author='Per List' --after='2000-01-01' --word-diff --unified=0 | sed -ne '/^\[-/p' | sed -e '/{+/d' | wc -l

Git CRLF mess:
https://stackoverflow.com/questions/10418975/how-to-change-line-ending-settings

2015/04/15

Simple screen recorder

# vi /etc/yum.repos.d/nickth-ssr-fedora-20.repo


[nickth-ssr]
name=Copr repo for ssr owned by nickth
baseurl=https://copr-be.cloud.fedoraproject.org/results/nickth/ssr/fedora-$releasever-$basearch/
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://copr-be.cloud.fedoraproject.org/results/nickth/ssr/pubkey.gpg
enabled=1

# yum install ssr-libs.x86_64 ssr.x86_64

Source:
http://www.maartenbaert.be/simplescreenrecorder/

2015/03/20

2015/03/19

Parallel shell (bash)

find /path/to/files | xargs -n1 -P8 sh -c 'foo $0 && bar $0 || baz $0'

The -n is how many inputs to give each process and the -P indicates the number of processes to run in parallel.
Also important to be aware of is that such a parallel pipeline doesn't guarantee delivery order, but this isn't a problem if you are used to dealing with distributed processing systems.

xargs --max-procs=$(getconf _NPROCESSORS_ONLN)

2015/03/02

Modern Perl

perlbrew - brew your versions of perl
carton - local application
pinto -
cpanm - local cpan modules

2015/02/18

Perl modules

Daemonize:
Proc::Daemon
Proc::Async

Debug:
Debug::Statements
Smart::Comments

Dependencies:
Module::Dependency
Perl::PrereqInstaller
Devel::Modlist
Module::ScanDeps

Dump:
Data::Dumper
Data::Dump

File:
File::Basename
File::Copy
File::Find
File::Path - `rmtree`, create or remove directory trees
File::Spec - operations on file names
File::Temp
Path::Class - path specification manipulation
Path::Class::Rule
File::Slurper - minimal needs
Path::Tiny - average needs
IO::All - overkill

IPC:
IPC::System::Simple - `system`, `capturex`
IO::Capture::Stdout/err - capture stdout/err from Perl statements

Profile:
Devel::Timer
Devel::NYTProf

Test:
Test::More
Test::Class
Test::Unit::TestCase
Devel::Cover
Test::Mock::Signature
Test::WWW::Mechanize::PSGI
Test::Power

Validation:
Validator::LIVR

WebSocket:
Protocol::WebSocket


2015/01/29

Pretty print JSON

$ cat json | perl -MJSON::XS -ne 'print JSON::XS->new->pretty(1)->encode(decode_json $_)'

In ~/.bashrc:
alias jsonn='perl -MJSON::XS -ne "print JSON::XS->new->pretty(1)->encode(decode_json \$_)"'

Cat in file

$ cat > json <<EOF
{ "json": "data" }
EOF

Generate passwords

$ pwgen -y 20 1

Install Fedora

yum install vim kate speedcrunch firefox clementine \
evolution-ews pidgin git libreoffice icedtea-web dia \
ack perl-JSON-XS perl-App-cpanminus wine hplip

yum remove amarok libreoffice

rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm

yum install gstreamer gstreamer-ffmpeg \
gstreamer-plugins-bad gstreamer-plugins-bad-free \
gstreamer-plugins-bad-free-extras \
gstreamer-plugins bad-nonfree \
gstreamer-plugins-base gstreamer-plugins-good \
gstreamer-plugins-ugly \
faad2 faac libdca wget \
compat-libstdc++-33 compat-libstdc++-296 \
xine-lib-extras-freeworld

firefox add-ons: Status-4-Evar Zoom-Page

skype.com

package-cleanup --leaves | xargs -l1 yum -y remove

Add to .bashrc:
export PERL5LIB=$HOME/perl5/lib/perl5:$PERL5LIB

curl -L http://cpanmin.us | perl - local::lib

List devices

$ lsblk

Cat "eats" lines

$ reset
OR try to change terminal:
$ echo $TERM
xterm-256color
$ export TERM=linux
$ echo $TERM
$ reset

Maybe after that:
$ stty arrays ^?

2015/01/13

Find path

find -wholename '*Dir/File.pm'
find -iwholename '*Permission*'
find haystack | grep -i needle

find lib -type f -iname '*pmoops' -delete