If you plan to keep coming to this tutorial session:
- Send email to [michael.brauweiler@posteo.de]
- Thursday: Use subject
[FDE T-7]
.- Friday: Use subject
[FDE T-9]
.
Interface to the system reference manuals.
man man
Concatenate files and print on the standard output.
cat [OPTION]... [FILE]...
cat random-names.txt
tac random-names.txt
Compares two sorted files line by line.
comm [OPTION]... FILE1 FILE2
cat random-names.txt |
shuf |
head |
sort > meeting1.txt
cat random-names.txt |
shuf |
head |
sort > meeting2.txt
comm meeting1.txt meeting2.txt
Join lines of two files on a common sorted field.
join [OPTION]... FILE1 FILE2
cat random-names.txt |
cut -d " " -f1 |
cat -n > first_names.txt
cat random-names.txt |
cut -d " " -f2 |
cat -n > last_names.txt
join first_names.txt last_names.txt
Sort lines of text files by a key/column.
sort [OPTION]... [FILE]...
sort random-names.txt
Output the first part/last part of files.
head [OPTION]... [FILE]...
head random-names.txt
tail random-names.txt
Report or omit (subsequent) repeated lines.
uniq [OPTION]... [INPUT [OUTPUT]]
cat random-names.txt \
<(head random-names.txt) |
sort > repeated-names.txt
uniq repeated-names.txt
Print newline, word, and byte counts for each file.
wc [OPTION]... [FILE]...
wc random-names.txt
# Counts only newline characters
wc -l random-names.txt
wc -w random-names.txt
echo -n "😀" | wc -c
echo -n "😀" | wc -m
Print a sequence of numbers.
seq [OPTION]... LAST seq [OPTION]... FIRST LAST seq [OPTION]... FIRST INCREMENT LAST
seq 10
seq 5 10
seq -10 2 10
Print lines matching a pattern.
grep [OPTION...] PATTERNS [FILE...]
grep -E '(.).+ \1.+' random-names.txt
Stream editor for filtering and transforming text.
sed [OPTION]... {script} [input-file]...
cat random-names.txt |
sed -E 's/(.+) (.+)/\2 \1/'
Pattern scanning and processing language.
awk '{print $2,$1}' random-names.txt
There are three ways to combine programs:
A sequence of one or more commands separated by one of the control operators |
or |&
.
[time [-p]] [ ! ] command [ [| | |&] command ... ]
command1 | command2
forwards standard output of command1
to standard input of command2
.command1 |& command2
forwards standard output and standard error of command1
to standard input of command2
.command1 2>&1 | command2
Sequence of one or more pipelines combined by operators:
;
, &
, &&
, or ||
.;
, &
<newline>
;
- executes sequentially&
- executes in background&&
- AND||
- OR# command2 is only executed if command1 returns successfully (status zero)
command1 && command2
command1 || command2
command1 <(command2) <(command3)
for num in {1..100} ; do
out=""
if ! (( $num % 3)) ; then out="Fizz"; fi
if ! (( $num % 5)) ; then out="${out}Buzz"; fi
echo ${out:-$num}
done
cut -d' ' -f2 random-names.txt
awk -F" " '{ print $2 }' random-names.txt
[...] helps the user get the output one page at a time [...]
Algebraic notation for characterizing a set of strings.
[wW]
: w
or W
Matches woodchuck or Woodchuck.
yours|mine
: yours
or mine
a|b|c
: same as [abc]
smil(ey|ies)
: smiley
or smilies
[a-z]
: lowercase letter[A-Z]
: uppercase letters[0-9]
: numbers[a-z5-9]
[^A-Z]
: not an uppercase letter[^Ss]
: neither S
or s
colou?r
: color
or colour
o*h!
: h!
or oh!
or ooh!
...o+h!
: oh!
or ooh!
...a{3,5}h
: aaah
or aaaah
or aaaaah
beg.n
: begin
or begun
or beg3n
^
: start of line$
: end of line\b
: word boundary
\bthe\b
the
worldTask: Build a email address matching regex. Should match addresses in mails_match.txt
not mails_dont_match.txt
.
grep -E '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' mails_match.txt |
wc -l
Regex has the concept of groups:
(...)
(?:...)
Task 1: Use non-capturing groups to match mac addresses in random-mac.txt
. Task 2: Use capturing groups to swap first name and last name in random-names.txt
.
cat random-mac.txt |
grep -E '(?:[0-9A-F]{2}:){5}[0-9A-F]{2}'
cat random-names.txt |
sed -E 's/(.+) (.+)/\2, \1/'
cat random-names.txt |
awk '{print $2", "$1}'
cat random-names.txt |
perl -n -e'/^([A-z]+) ([A-z]+)$/ && print $2, ", ",$1, "\n"'