Skip to content

Commit

Permalink
updates for version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
learnbyexample committed May 8, 2020
1 parent 65df275 commit 024e6c2
Show file tree
Hide file tree
Showing 47 changed files with 1,717 additions and 16 deletions.
35 changes: 31 additions & 4 deletions code_snippets/Builtin_functions.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## length

awk 'BEGIN{print length("road"); print length(123456)}'

printf 'fox\ntiger\n' | awk '{print length()}'
Expand All @@ -8,6 +10,10 @@ echo 'αλεπού' | awk '{print length()}'

echo 'αλεπού' | awk -b '{print length()}'

echo 'αλεπού' | LC_ALL=C awk '{print length()}'

## Array sorting

awk 'BEGIN{a["z"]=1; a["x"]=12; a["b"]=42; for(i in a) print i, a[i]}'

awk 'BEGIN{PROCINFO["sorted_in"] = "@ind_str_asc";
Expand All @@ -16,6 +22,11 @@ awk 'BEGIN{PROCINFO["sorted_in"] = "@ind_str_asc";
awk 'BEGIN{PROCINFO["sorted_in"] = "@val_num_asc";
a["z"]=1; a["x"]=12; a["b"]=42; for(i in a) print i, a[i]}'

awk 'BEGIN{PROCINFO["sorted_in"] = "@ind_str_asc"}
{a[$2]=$0} END{for(k in a) print a[k]}' table.txt

## split

printf ' one \t two\t\t\tthree ' | awk '{split($0, a); print a[2]}'

s='Joe,1996-10-25,64,78'
Expand All @@ -25,7 +36,7 @@ echo "$s" | awk -F, '{split($2, d, "-"); print $1 " was born in " d[1]}'
s='air,water,12:42:3'

echo "$s" | awk -F, '{n=split($NF, a, ":");
for(i=1; i<=n; i++) print $1,$2,a[i]}'
for(i=1; i<=n; i++) print $1, $2, a[i]}'

s='Sample123string42with777numbers'

Expand All @@ -41,10 +52,14 @@ cat marks.txt
awk 'BEGIN{OFS="\t"; split("DCBAS", g, //)}
{$(NF+1) = NR==1 ? "Grade" : g[int($NF/10)-4]} 1' marks.txt

## patsplit

s='eagle,"fox,42",bee,frog'

echo "$s" | awk '{patsplit($0, a, /"[^"]*"|[^,]*/); print a[2]}'

## substr

echo 'abcdefghij' | awk '{print substr($0, 1, 5)}'

echo 'abcdefghij' | awk '{print substr($0, 4, 3)}'
Expand All @@ -57,6 +72,8 @@ echo 'abcdefghij' | awk -v FS= '{print $3}'

echo 'abcdefghij' | awk -v FS= '{print $3, $5}'

## match

s='051 035 154 12 26 98234'

echo "$s" | awk 'match($0, /[0-9]{4,}/){print substr($0, RSTART, RLENGTH)}'
Expand All @@ -72,6 +89,8 @@ s='42 foo-5, baz3; x-83, y-20: f12'
echo "$s" | awk '{ while( match($0, /([0-9]+),/, m) ){print m[1];
$0=substr($0, RSTART+RLENGTH)} }'

## index

cat eqns.txt

awk '/i*(t+9-g)/' eqns.txt
Expand All @@ -92,6 +111,8 @@ echo 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)'

echo 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])'

## system

awk 'BEGIN{system("echo Hello World")}'

wc table.txt
Expand All @@ -102,20 +123,24 @@ awk 'BEGIN{system("seq 10 | paste -sd, > out.txt")}'

cat out.txt

cat f2.txt
cat t2.txt

echo 'f1,f2,f3' | awk -F, '{system("cat " $2 ".txt")}'
echo 'f1,t2,f3' | awk -F, '{system("cat " $2 ".txt")}'

ls xyz.txt

echo $?

awk 'BEGIN{s=system("ls xyz.txt"); print "Exit status: " s}'

awk 'BEGIN{sum = 3.1428 + 10; print sum}'
## printf and sprintf

awk 'BEGIN{print OFMT}'

awk 'BEGIN{sum = 3.1428 + 100; print sum}'

awk 'BEGIN{OFMT="%.5f"; sum = 3.1428 + 100; print sum}'

awk 'BEGIN{sum = 3.1428 + 10; printf "%f\n", sum}'

awk 'BEGIN{sum = 3.1428 + 10; printf "%.3f\n", sum}'
Expand Down Expand Up @@ -154,6 +179,8 @@ awk 'BEGIN{printf "n%%d gives the remainder\n"}'

awk 'BEGIN{pi = 3.14159; s = sprintf("%010.3f", pi); print s}'

## Redirecting print output

seq 6 | awk 'NR%2{print > "odd.txt"; next} {print > "even.txt"}'

cat odd.txt
Expand Down
14 changes: 12 additions & 2 deletions code_snippets/Control_Structures.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## if-else

awk '/^b/{print; if($NF>0) print "------"}' table.txt

awk '/^b/{print; if($NF>0) print "------"; else print "======"}' table.txt
Expand All @@ -6,6 +8,8 @@ seq 6 | awk '{ORS = NR%3 ? "-" : RS} 1'

awk '/^b/{print; print($NF>0 ? "------" : "======")}' table.txt

## loops

awk 'BEGIN{for(i=2; i<7; i+=2) print i}'

awk -v OFS=, '{for(i=1; i<=NF; i++) if($i ~ /^[bm]/) $i="["$i"]"} 1' table.txt
Expand All @@ -20,17 +24,23 @@ awk 'BEGIN{i=6; while(i>0){print i; i-=2}}'

echo 'titillate' | awk '{while(gsub(/til/, "")) print}'

echo 'titillate' | awk '{do{print} while(gsub(/til/, ""))}'

## next

awk '/\<par/{print "%% " $0; next} {print /s/ ? "X" : "Y"}' word_anchors.txt

## exit

seq 3542 4623452 | awk 'NR==2452{print; exit}'

echo $?

awk '/^br/{print "Invalid input"; exit 1}' table.txt
awk '/^br/{print "Invalid input"; exit 1}' table.txt

echo $?

awk 'FNR==2{print; exit}' table.txt greeting.txt
awk 'FNR==2{print; exit}' table.txt greeting.txt

awk 'BEGIN{print "hi"; exit; print "hello"}
/^b/;
Expand Down
6 changes: 6 additions & 0 deletions code_snippets/Dealing_with_duplicates.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
## Whole line duplicates

cat purchases.txt

awk '{print +a[$0] "\t" $0; a[$0]++}' purchases.txt

awk '!a[$0]++' purchases.txt

## Column wise duplicates

cat duplicates.txt

awk -F, '!seen[$NF]++' duplicates.txt

awk -F, '!seen[$1,$3]++' duplicates.txt

## Duplicate count

awk -F, '++seen[$2]==2' duplicates.txt

awk -F, '++seen[$NF]==3' duplicates.txt
Expand Down
26 changes: 25 additions & 1 deletion code_snippets/Field_separators.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Default field separation

cat table.txt

awk '$1 ~ /^b/{print $4}' table.txt
Expand All @@ -18,7 +20,7 @@ echo ' a b c ' | awk '{print $NF "."}'

printf ' one \t two\t\t\tthree ' | awk '{print NF}'

printf ' one \t two\t\t\tthree ' | awk '{print $2}'
printf ' one \t two\t\t\tthree ' | awk '{print $2 "."}'

awk 'BEGIN{printf "%.16f\n", 2.999999999999999}'

Expand All @@ -28,6 +30,8 @@ awk '{print $2.999999999999999}' table.txt

awk '{print $2.9999999999999999}' table.txt

## Input field separator

echo 'goal:amazing:whistle:kwality' | awk -F: '{print $1}'

echo 'goal:amazing:whistle:kwality' | awk -F: '{print $NF}'
Expand Down Expand Up @@ -60,6 +64,14 @@ echo ' a b c ' | awk -F' ' '{print NF}'

echo ' a b c ' | awk -F'[ ]' '{print NF}'

echo 'RECONSTRUCTED' | awk -F'[aeiou]+' -v IGNORECASE=1 '{print $1}'

echo 'RECONSTRUCTED' | awk -F'e' -v IGNORECASE=1 '{print $1}'

echo 'RECONSTRUCTED' | awk -F'[e]' -v IGNORECASE=1 '{print $1}'

## Output field separator

awk '{print $1, $3}' table.txt

echo 'goal:amazing:whistle:kwality' | awk -F: -v OFS=: '{print $2, $NF}'
Expand All @@ -78,6 +90,8 @@ echo 'Sample123string42with777numbers' | awk -F'[0-9]+' -v OFS=, '1'

echo 'Sample123string42with777numbers' | awk -F'[0-9]+' -v OFS=, '{$1=$1} 1'

## Manipulating NF

echo 'goal:amazing:whistle:kwality' | awk -F: -v OFS=, '{NF=2} 1'

echo 'goal:amazing:whistle:kwality' | awk -F: -v OFS=: '{$(NF+1)="sea"} 1'
Expand All @@ -86,6 +100,8 @@ echo 'goal:amazing:whistle:kwality' | awk -F: -v OFS=: '{$8="go"} 1'

echo 'goal:amazing:whistle:kwality' | awk -F: -v OFS=: '{NF=-1} 1'

## FPAT

s='Sample123string42with777numbers'

echo "$s" | awk -v FPAT='[0-9]+' '{print $2}'
Expand All @@ -98,6 +114,14 @@ echo "$s" | awk -F, '{print $2}'

echo "$s" | awk -v FPAT='"[^"]*"|[^,]*' '{print $2}'

echo 'Read Eat Sleep' | awk -v FPAT='e' '{print NF}'

echo 'Read Eat Sleep' | awk -v IGNORECASE=1 -v FPAT='e' '{print NF}'

echo 'Read Eat Sleep' | awk -v IGNORECASE=1 -v FPAT='[e]' '{print NF}'

## FIELDWIDTHS

cat items.txt

awk -v FIELDWIDTHS='8 4 6' '{print $2}' items.txt
Expand Down
24 changes: 24 additions & 0 deletions code_snippets/Gotchas_and_Tips.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
## Prefixing $ for variables

awk -v word="cake" '$2==$word' table.txt

awk -v word="cake" '$2==word' table.txt

awk -v field=2 '{print $field}' table.txt

## Dos style line endings

printf 'mat dog\n123 789\n' | awk '{print $2, $1}'

printf 'mat dog\r\n123 789\r\n' | awk '{print $2, $1}'
Expand All @@ -14,6 +18,8 @@ printf 'mat dog\r\n123 789\r\n' | awk -v RS='\r\n' '{print $2, $1}'

printf 'mat dog\r\n123 789\r\n' | awk -v RS='\r\n' '{sub(/$/, ".")} 1'

## Word boundary differences

echo 'I have 12, he has 2!' | awk '{gsub(/\y..\y/, "[&]")} 1'

echo 'I have 12, he has 2!' | awk '{gsub(/\<..\>/, "[&]")} 1'
Expand All @@ -24,6 +30,8 @@ echo 'hi log_42 12b' | awk '{gsub(/\</, ":")} 1'

echo 'hi log_42 12b' | awk '{gsub(/\>/, ":")} 1'

## Relying on default initial value

awk '{sum += $NF} END{print sum}' table.txt

awk '{sum += $NF} ENDFILE{print FILENAME ":" sum}' table.txt
Expand All @@ -32,6 +40,16 @@ awk '{sum += $NF} ENDFILE{print FILENAME ":" sum}' table.txt marks.txt

awk '{sum += $NF} ENDFILE{print FILENAME ":" sum; sum=0}' table.txt marks.txt

## Code in replacement section

awk '{sub(/^(br|ye)/, ++c ") &")} 1' table.txt

awk '/^(br|ye)/{sub(/^/, ++c ") ")} 1' table.txt

awk '{gsub(/\<b/, ++c ") &")} 1' table.txt

## Forcing numeric context

awk '{sum += $NF} END{print sum}' table.txt

awk '{sum += $1} END{print sum}' table.txt
Expand All @@ -40,10 +58,14 @@ awk '{sum += $1} END{print sum}' /dev/null

awk '{sum += $1} END{print +sum}' /dev/null

## Forcing string context

echo '5 5.0' | awk '{print ($1==$2 ? "same" : "different"), "number"}'

echo '5 5.0' | awk '{print ($1""==$2 ? "same" : "different"), "string"}'

## Negative NF

cat varying.txt

awk '{NF -= 2} 1' varying.txt
Expand All @@ -54,6 +76,8 @@ awk '{print $(NF-2)}' varying.txt

awk 'NF>2{print $(NF-2)}' varying.txt

## Faster execution

time awk '/^([a-d][r-z]){3}$/' /usr/share/dict/words > f1

time LC_ALL=C awk '/^([a-d][r-z]){3}$/' /usr/share/dict/words > f2
Expand Down
10 changes: 7 additions & 3 deletions code_snippets/Inplace_file_editing.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
cat greeting.txt
## Without backup

awk -i inplace '{print NR ". " $0}' greeting.txt
cat greet.txt

cat greeting.txt
awk -i inplace '{print NR ". " $0}' greet.txt

cat greet.txt

cat f1.txt

Expand All @@ -14,6 +16,8 @@ cat f1.txt

cat f2.txt

## With backup

cat f3.txt

awk -i inplace -v inplace::suffix='.bkp' -v OFS=, '{$1=$1} 1' f3.txt
Expand Down
12 changes: 9 additions & 3 deletions code_snippets/Installation_and_Documentation.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
wget https://ftp.gnu.org/gnu/gawk/gawk-5.0.1.tar.xz
## Installation

tar -Jxf gawk-5.0.1.tar.xz
wget https://ftp.gnu.org/gnu/gawk/gawk-5.1.0.tar.xz

cd gawk-5.0.1/
tar -Jxf gawk-5.1.0.tar.xz

cd gawk-5.1.0/

./configure

Expand All @@ -14,7 +16,11 @@ type -a awk

awk --version | head -n1

## Documentation

man awk

## Options overview

awk --help

8 changes: 7 additions & 1 deletion code_snippets/Multiple_file_input.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
## BEGINFILE, ENDFILE and FILENAME

awk 'BEGINFILE{print "--- " FILENAME " ---"} 1' greeting.txt table.txt

awk 'ENDFILE{print $0}' greeting.txt table.txt

awk '/3/{print FILENAME; nextfile}' f[1-3].txt greeting.txt
## nextfile

awk '/I/{print FILENAME; nextfile}' f[1-3].txt greeting.txt

awk 'BEGINFILE{m1=m2=0} /o/{m1=1} /at/{m2=1}
m1 && m2{print FILENAME; nextfile}' f[1-3].txt greeting.txt

## ARGC and ARGV

awk 'BEGIN{for(i=0; i<ARGC; i++) print ARGV[i]}' f[1-3].txt greeting.txt

awk 'BEGIN{for(i=0; i<ARGC; i++) print ARGV[i]}' table.txt n=5 greeting.txt
Expand Down
Loading

0 comments on commit 024e6c2

Please sign in to comment.