Skip to content

Instantly share code, notes, and snippets.

@cdosborn
Created June 10, 2015 20:05
Show Gist options
  • Save cdosborn/22085c625cb5ef2abdf8 to your computer and use it in GitHub Desktop.
Save cdosborn/22085c625cb5ef2abdf8 to your computer and use it in GitHub Desktop.
intro.awk
# Big ideas
#
# - 2 types of shell tools
# - Compute (ls, rm, find, grep, vim ..)
# - Format (sort, tail, cut, uniq, sed, ..)
# - awk is the ultimate formatting tool
# - a complete prog lang for text processing
# - handle state on the cmd line
# - 2 major datatypes
# - avoids pitfalls of other tools
# - Python's ease, sed's power
# - Good number support
# - Gotchas
# - 1 based indexing
# - no local variabels .__.
# Patterns and actions
# { print }
# /^a.*/ { print }
# BEGIN { print "Beginning" } { print } END { print "End"; }
# BEGIN { print "Hello World!" }
# Variables and common sense
# { print $2 }
# { print $0 } # cat
# { print NR, $0 } # line number
# NR == 2 { print "Only", NR ",", $0 }
# { print NF, $0 } # number of fields
# { print length }
# { # reverse each line
# for (i = NF; i > 0; i--) {
# printf $i" ";
# }
# print "";
# }
# Print empty for empty lines
# length > 0 { print }
# /^$/ { print "empty" }
# Math
# /[0-9.]/ {
# sum = 0;
# for (i = 1; i <= length; i++) {
# sum += $i # += is a numeric operation
# }
# print "sum:", sum
# }
# Shell like interface
# BEGIN {
# cmd="ls ./"
# while(cmd | getline line)
# print line
# }
# Print the environment
# BEGIN {
# printArray(SYMTAB, 1)
# }
# function printArray(arr, level, i) {
# if (!isarray(arr)) {
# return;
# }
# for (i in arr) {
# printf getIndent(level) i
# if (isarray(arr[i]))
# printf ":\n"
# else
# printf "\n"
# printArray(arr[i], level + 1);
# }
# }
# function getIndent(num, indent, i) {
# for (i = 1; i < num; i++) {
# indent = (indent "\t");
# }
# return indent
# }
#:nnoremap -x :silent !gawk -f intro.awk < input.txt > output.txt<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment