├── .gitignore ├── README.md ├── cookbook.org └── cookbook.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | _minted-cookbook 2 | .DS_Store 3 | *.tex 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # awk-cookbook 2 | 3 | Useful AWK one-liners for your day-to-day tasks. 4 | 5 | ## License 6 | 7 | Use it as you see fit :) 8 | -------------------------------------------------------------------------------- /cookbook.org: -------------------------------------------------------------------------------- 1 | #+LaTeX_CLASS: article 2 | #+LaTeX_HEADER: \usepackage[T1]{fontenc} 3 | #+LaTeX_HEADER: \usepackage{libertine} 4 | #+LaTeX_HEADER: \renewcommand*\oldstylenums[1]{{\fontfamily{fxlj}\selectfont #1}} 5 | #+LaTeX_HEADER: \usepackage{lmodern} 6 | #+LaTeX_HEADER: \usepackage{minted} 7 | #+LaTeX_HEADER: \usemintedstyle{emacs} 8 | #+LaTeX_HEADER: \newminted{bash}{fontsize=\footnotesize} 9 | 10 | #+LaTeX_HEADER:\usepackage{titlesec} 11 | #+LaTeX_HEADER:\titlespacing*{\section} 12 | #+LaTeX_HEADER:{0pt}{5.5ex plus 1ex minus .2ex}{4.3ex plus .2ex} 13 | #+LaTeX_HEADER: \vspace*{\stretch{4.0}}\titlespacing*{\subsection} 14 | #+LaTeX_HEADER:{0pt}{5.5ex plus 1ex minus .2ex}{4.3ex plus .2ex} 15 | 16 | #+LaTeX_HEADER: \begin{titlepage} 17 | #+LaTeX_HEADER: \vspace*{\stretch{1.0}} 18 | #+LaTeX_HEADER: \begin{center} 19 | #+LaTeX_HEADER: \Huge\textbf{AWK Cookbook}\\ 20 | #+LaTeX_HEADER: \large\textit{Kiran Gangadharan} 21 | #+LaTeX_HEADER: \end{center} 22 | #+LaTeX_HEADER: \vspace*{\stretch{2.0}} 23 | #+LaTeX_HEADER: \end{titlepage} 24 | 25 | * Recipes 26 | ** Count lines in a file 27 | 28 | #+BEGIN_SRC sh 29 | awk 'END { print NR }' 30 | #+END_SRC 31 | 32 | ** Count no of words in a file 33 | 34 | #+BEGIN_SRC sh 35 | awk '{ total = total + NF } END { print total }' 36 | #+END_SRC 37 | 38 | ** Find sum of columns for each row 39 | 40 | #+BEGIN_SRC sh 41 | awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}' 42 | #+END_SRC 43 | 44 | ** Find and replace in file 45 | 46 | #+BEGIN_SRC sh 47 | # Replace only first instance in line 48 | awk '{sub(/HTML/, "html")}; 1' data.txt > data_new.txt 49 | 50 | # Replace all instances in line 51 | awk '{gsub(/HTML/, "html")}; 1' data.txt > data_new.txt 52 | #+END_SRC 53 | 54 | ** Remove duplicate lines from a file 55 | 56 | #+BEGIN_SRC sh 57 | awk '!a[$0]++' data.txt 58 | #+END_SRC 59 | 60 | ** Print all lines containing "Programming" 61 | 62 | #+BEGIN_SRC sh 63 | awk '/Programming/ { n++ }; END { print n+0 }' 64 | #+END_SRC 65 | 66 | ** Prefixing line with increasing number 67 | 68 | #+BEGIN_SRC sh 69 | awk '{print FNR ". " $0}' data.txt 70 | #+END_SRC 71 | 72 | ** Print account name and role from /etc/passwd 73 | 74 | #+BEGIN_SRC sh 75 | awk -F : '/^[^(#|_)]/ {print $1, "=>", $5}' /etc/passwd 76 | #+END_SRC 77 | 78 | ** Find the line containing largest value in the first column 79 | 80 | #+BEGIN_SRC sh 81 | awk '$1 > max {max=$1; maxline=$0}; END{ print maxline}' data.txt 82 | #+END_SRC 83 | 84 | ** Swap first two columns of every line 85 | 86 | #+BEGIN_SRC sh 87 | awk '{temp=$1; $1=$2; $2=temp} 1' data.txt 88 | #+END_SRC 89 | 90 | ** Delete second column from every line 91 | 92 | #+BEGIN_SRC sh 93 | awk '{ $2 = ""; print }' data.txt 94 | #+END_SRC 95 | 96 | ** Perform search similar to SQL 'where' 97 | 98 | #+BEGIN_SRC sh 99 | awk '$2 == "hello"' data.txt 100 | #+END_SRC 101 | ** Delete leading whitespace from each line 102 | 103 | #+BEGIN_SRC sh 104 | awk '{sub(/^[ \t]+/, ""); print}' 105 | #+END_SRC 106 | ** Delete trailing whitespace from each line 107 | 108 | #+BEGIN_SRC sh 109 | awk '{sub(/[ \t]+$/, ""); print}' 110 | #+END_SRC 111 | ** Replace all instances of "foo" with "bar" 112 | 113 | #+BEGIN_SRC sh 114 | awk '{gsub(/foo/,"bar");print}' 115 | #+END_SRC 116 | -------------------------------------------------------------------------------- /cookbook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirang89/awk-cookbook/deb918cc667403083920d0cefd27b19affe8ef94/cookbook.pdf --------------------------------------------------------------------------------