#+TITLE: Using gawk in command-line #+DATE: 2022-03-15 #+OPTIONS: num:nil toc:nil author:nil #+PROPERTY: header-args:bash :exports both :session *bsh* :tangle yes #+PROPERTY: header-args:bash+ :cmdline :results output * gawk stands for GNU-awk ~awk~ is a data driven program that search text in files using a pattern. After finding the pattern ~awk~ performs operations on these files. ~awk~ is not bash, but since you can use it from a shell, let's talk about ~awk~ in this tutorial. ** Syntaxis Use the following and apply to files or to standard input ('INPUT') #+begin_example bash awk PATTERN { ACTION } INPUT #+end_example ~awk~ can be run directly in the command-line, or you can write scripts and run them. #+begin_src bash set +H #disabling c-shell-style to avoid treat '!' as special character #+end_src #+RESULTS: | compartido@compartido: | ~/Documentos/GNU/bash/bash-tutorial | | compartido@compartido: | ~/Documentos/GNU/bash/bash-tutorial | #+begin_src bash awk 'BEGIN { print " Don\47t Panic!" }' #+end_src #+RESULTS: | Don't | Panic! | To run the program from a file, use the 'f' argument followed by the name of the file. #+begin_src bash awk -f file.awk #+end_src #+RESULTS: | Dont | Panic! | ~awk~ scripts use the following before the code #+begin_example bash #! /bin/awk -f #+end_example Then, make the file executable ('chmod +x file.awk') ** Data Check in awklib/eg/data #+begin_src bash :results output awk '/li/ { print $0}' mail-listc #+end_src #+RESULTS: : Amelia 555-5553 amelia.zodiacusque@gmail.comF : Broderick555-0542 broderick.aliquotiens@yahoo.com R : Julie555-6699 julie.perscrutabor@skeeve.com F : Samuel 555-3430 samuel.lanceolis@shu.eduA : compartido@compartido: ~/Documentos/GNU/bash/bash-tutorial Printing is the default action. Thus, when printing is omitted, ~awk~ as action in the code, the output will still be printed. #+begin_src bash awk 'length($0) > 50' mail-listc #+end_src #+RESULTS: : Anthony 555-3412 anthony.asserturo@hotmail.com A : Broderick555-0542 broderick.aliquotiens@yahoo.com R : compartido@compartido: ~/Documentos/GNU/bash/bash-tutorial ** awk examples Compare two text files $ awk ' NR==FNR { # process b.txt or the first file seen[$0] # hash words to hash seen next # next word in b.txt } # process a.txt or all files after the first !($0 in seen)' b.txt a.txt # if word is not hashed to seen, output it awk ' NR==FNR { seen[$0] next } !($0 in seen)' diagnostico_es_2022-03-21.org ../reunion/reunion_2022_03_20.org git --diff diagnostico_es_2022-03-21.org ../reunion/reunion_2022_03_20.org * References - ~awk~ info manual