Written by Luka Kerr on November 12, 2018

Unix

wc

# get number of words
wc -w file.txt

# get number of characters
wc -c file.txt

# get number of lines
wc -l file.txt
# get top 10 lines of file
head file.txt

# get top 5 lines of file
head -5 file.txt

tail

# get last 10 lines of file
tail file.txt

# get last 5 lines of file
tail -5 file.txt

egrep

# match a regex pattern
egrep '\d' file.txt

# ignore case
egrep -i 'word' file.txt

# display lines that don't match the pattern
egrep -v '\d' file.txt

tr

# replace source characters by destination characters
tr 'srcChars' 'destChars' < file.txt

# delete all characters in source characters
tr -d 'srcChars' < file.txt

cut

# print the first column, seperated by |
cut -d'|' -f1 file.txt

sort

# sort numerically
sort -n file.txt

# sort dictionary order
sort -d file.txt

# sort in reverse
sort -r file.txt

sed

# substitute all occurances of digit with DIGIT
sed -e '/\d/DIGIT/g' file.txt

find

# find all html files under /home
find /home -name '*.html' -print

# execute command on found files
find  /home -name '*.html' -exec ls -l {} \;

Regex

Symbol Meaning
^ Anchor to start of line
$ Anchor to end of line
\w Word
\d Digit
. Any character except \n
(..) Group
[ABC] A or B or C
[^ABC] Not (A or B or C)
[a-z] Characters from a to z
* 0 or more
+ 1 or more
? 0 or 1
{3} Exactly 3
{3,} 3 or more
{3, 5} 3, 4 or 5
\ Escape next character

Shell

#!/bin/sh

# printing
echo "whatever"

# cli arguments
$1   # first cli argument
$#   # number of cli arguments

for arg in "$@"   # iterate over cli arguments
do
  echo $arg
done

# redirections
2>      # redirect stderr
2>&1 >  # redirect stderr and stdout

# variables
x=5
echo "x = $x"

# conditionals
=     # == string comparison
!=    # != string comparison
-eq   # == numeric comparison
-ne   # != numeric comparison
-lt   # < numeric comparison
-gt   # > numeric comparison

# statements
if [[ 5 -gt 3 ]]   # if statement
then
  echo "do something"
elif [[ 5 -lt 3 ]]
then
  echo "do something"
fi

while true   # while loop
do
  echo "do something"
done

case $# in   # case statement
0) echo "do something" ;;
1) echo "do something" ;;
*) echo "do something" ;;
esac

# math
sum=0
sum=`expr $sum + 3`

Perl

#!/usr/bin/perl

use warnings;
use strict;

# printing
print "whatever";

# cli arguments
@ARVG   # array of cli arguments

for my $arg (@ARGV) {   # iterate over cli arguments
  print "$arg\n";
}

# variables
$x = 5
print "x = $x\n";

# read from STDIN or input file
while ($line = <>) {
  print $line;
}

# open, read and close a file
open F, '<', "file.txt" or die "$!";

for my $line (<F>) {
  print $line;
}

close F;

# regex
if ($var =~ /[\d]/)         # if regex matches
if ($var !~ /[\d]/)         # if regex doesn't match
if ($var =~ /(\d)-(a-z)/)   # regex groups - matches are stored in $1 to $9
$var =~ tr/[a-z]/[A-Z]/     # convert to uppercase
$var =~ s/123/789/          # replace 123 with 789

JavaScript

// Read from STDIN line by line
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', (line) => {
  console.log(line);
});
// Read command line arguments
var args = process.argv.slice(2);

args.forEach(val => {
  process.stdout.write(val);
});