@@ -605,6 +605,26 @@ each line if it matches some pattern. If it does, the action will be executed.
605
605
END {...}
606
606
#+END_SRC
607
607
608
+ *Note*: The ~BEGIN~ and ~END~ are known as *startup* and *cleanup* actions,
609
+ respectively. Each of them are executed only once. The ~BEGIN~ content is
610
+ executed before the input record is read in, while the ~END~ content is
611
+ executed after the input record is done being read in.
612
+
613
+ #+BEGIN_SRC sh
614
+ # List student directories
615
+ ls -l /home/courses/BMI565/students
616
+ #+END_SRC
617
+
618
+ #+RESULTS:
619
+
620
+ #+BEGIN_SRC sh
621
+ # Example pipe list of directories and count them
622
+ ls -l /home/courses/BMI565/students | awk '
623
+ BEGIN { print "Analyzing student directory..." }
624
+ /^drw/ { ++n }
625
+ END { print "There are", n, "number of directories." }'
626
+ #+END_SRC
627
+
608
628
*** Built-in variables
609
629
610
630
There are some built-in variables that can be used to make using ~awk~ more
@@ -621,7 +641,7 @@ columns/fields in the file, which may be useful in manipulating the file.
621
641
| NF | Number of fields/columns | ~awk '{print NR,"->",NF;}'~ |
622
642
| FILENAME | Name of current file | ~awk '{print FILENAME}'~ |
623
643
| FNR | Number of records rel. to current | ~awk '{print FILENAME, FNR;}'~ |
624
- | 0ドル | The entire line | ~awk '{print 0ドル;}` ~ |
644
+ | 0ドル | The entire line | ~awk '{print 0ドル;}' ~ |
625
645
| $n | The nth field number | ~awk '{print 1ドル;}'~ |
626
646
627
647
*** Examples
@@ -630,13 +650,25 @@ Now that we have the general syntax, let's try out some ~awk~ commands.
630
650
631
651
#+BEGIN_SRC sh
632
652
# Reminder of what the -l flag does
633
- ls -l
653
+ ls -l /home/courses/BMI565/students
654
+ #+END_SRC
655
+
656
+ #+BEGIN_SRC sh
657
+ # Examples of awk and potential extracting and manipulating information
658
+ ls -l /home/courses/BMI565/students | awk '{ print 6,ドル9ドル }'
659
+ #+END_SRC
660
+
661
+ #+BEGIN_SRC sh
662
+ # Do some manipulation of entries
663
+ ls -l /home/courses/BMI565/students | awk '{ print 7ドル * 2 }'
634
664
#+END_SRC
635
665
636
666
#+BEGIN_SRC sh
637
- # Examples of awk and potential actions
638
- ls -l | awk '{ print 6ドル " " 9ドル }'
639
- ls -l | awk '{ print 7ドル * 2 }'
667
+ # More advanced example
668
+ ls -l /home/courses/BMI565/students | awk '
669
+ BEGIN { print "Checking dates of user directories..." }
670
+ 8ドル < 15 { print $NF, "was edited earlier in that month" }
671
+ END { print "There were a total of", NR, "users." }'
640
672
#+END_SRC
641
673
642
674
*** Resources and more
@@ -647,6 +679,7 @@ Now that we have the general syntax, let's try out some ~awk~ commands.
647
679
- [[http://www.grymoire.com/Unix/Awk.html][Awk (Grymoire)]]
648
680
- [[https://ss64.com/bash/awk.html][awk or gawk (GNU awk)]]
649
681
- [[https://github.com/learnbyexample/Command-line-text-processing/blob/master/gnu_awk.md][Learn by Example awk]]
682
+ - [[https://www.gnu.org/software/gawk/manual/html_node/Using-BEGIN_002fEND.html][Startup and Cleanup Actions with BEGIN and END]]
650
683
651
684
** sed - edit streams of text
652
685
0 commit comments