Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 695a5ff

Browse files
Add useful notes on awk for better understanding
1 parent 7f8f8e6 commit 695a5ff

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

‎notes/unix_outline.org‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,26 @@ each line if it matches some pattern. If it does, the action will be executed.
605605
END {...}
606606
#+END_SRC
607607

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+
608628
*** Built-in variables
609629

610630
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.
621641
| NF | Number of fields/columns | ~awk '{print NR,"->",NF;}'~ |
622642
| FILENAME | Name of current file | ~awk '{print FILENAME}'~ |
623643
| 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ドル;}'~ |
625645
| $n | The nth field number | ~awk '{print 1ドル;}'~ |
626646

627647
*** Examples
@@ -630,13 +650,25 @@ Now that we have the general syntax, let's try out some ~awk~ commands.
630650

631651
#+BEGIN_SRC sh
632652
# 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 }'
634664
#+END_SRC
635665

636666
#+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." }'
640672
#+END_SRC
641673

642674
*** Resources and more
@@ -647,6 +679,7 @@ Now that we have the general syntax, let's try out some ~awk~ commands.
647679
- [[http://www.grymoire.com/Unix/Awk.html][Awk (Grymoire)]]
648680
- [[https://ss64.com/bash/awk.html][awk or gawk (GNU awk)]]
649681
- [[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]]
650683

651684
** sed - edit streams of text
652685

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /