Twitter

awk Tutorial -- 3 -- IF THEN ELSE

This blog is part of an awk Tutorial, you'll find the whole list of section already covered here.
Every language has an IF THEN ELSE structure to do things depending on if some condition(s) is(are) TRUE or FALSE and awk is no exception to that. The syntax is pretty simple:
if () {}  <== What to do if the condition is met
else {}   <== What to do if the condition is NOT met
Also awk uses the classic comparison operators other languages also use:
==    equal to
>     greater than
>=    greater than or equal
<     less than
<=    less than or equal
!     not
!=    not equal
~     contains
!~    do not contain
&&    AND
||    OR
We know that in an /etc/passwd file, the third column is the user id and we also know that the system users have a user id < 1000. Let's say we want to only list the non system users (user id >= 1000):
$ awk -F ":" '{if ($3 >= 1000) {print $1, $3}}' /etc/passwd
nobody 65534
opc 1000
ansible 1001
frdenis 1002
$
Note that you can pipe the output of your awk command to any other command as with any other classic Unix/Linux command:
$ awk -F ":" '{if ($3 >= 1000) {print $1, $3}}' /etc/passwd  | sort
ansible 1001
frdenis 1002
nobody 65534
opc 1000
$
Let's say we now want to highlight the users which have a user id equal to 0 (they then have root privileges) and those who do not have root privileges:
$ awk -F ":" '{if ($3 == 0) {print "This user has root privilege: "$1, $3} else {print "This is a regular user: "$1, $3}}' /etc/passwd
This user has root privilege: root 0
This is a regular user: opc 1000
This is a regular user: ansible 1001
This is a regular user: frdenis 1002
$
Below an example checking the user is really root:
$ awk -F ":" '{if ($3 == 0 && $1 == "root") {print "This user is the real root: "$0}}' /etc/passwd
This user is the real root: root:x:0:0:root:/root:/bin/bash
$
The ~ (contains) is also very powerful; let's say we want to show the users with bash as a shell but depending on the system bash may be in /bin, /usr/bin, etc... so we want to see if the 7th column contains bash whether it is /bin/bash or /usr/bin/bash or whatever path bash is in:
$ cat /etc/passwd | awk -F ":" '{if ($7 ~ "bash") {print $1, $3, $7}}'
root 0 /bin/bash
fred 1000 /sbin/bash
$
Here we have checked that the 7th column was containing the string bash but we can also use regular expressions; example below checks if the 7th column contains bin or sbin:
$ cat /etc/passwd | awk -F ":" '{if ($7 ~ /[s]?bin/) {print $1, $3, $7}}'
root 0 /bin/bash
daemon 1 /usr/sbin/nologin
bin 2 /usr/sbin/nologin
. . .
fred 1000 /sbin/bash
$
That's it for this one, you know IF THEN ELSE, congratulations!

4 comments:

  1. You've skipped over the 'pattern-action' rules in the Intro and in the Concepts posts, and now you dig down into the IF statement. I'm afraid you're not explaining idiomatic awk if you don't *start* with the pattern-action rules, or at least explain it before the IF statement.

    ReplyDelete
    Replies
    1. This is on purpose; indeed:
      - I am not sure that throwing many dev concepts is a good idea; it is like learning databases from theory, I have always given up myself, it is so off-putting; I like more showing by example which is also how I personally learn best
      - I am pretty sure that no one will go from section 1 to section X in order and it may be more likely use as a "How do I do that in AWK ?"

      Just my way of thinking,

      Regards,

      Delete
    2. It's not really 'many dev concept', it's the main idea of awk, that you have multiple "rules", each consisting of pattern and corresponding action, which is done only if the pattern matches. You even explain the custom cases of BEGIN and END patterns...

      Delete
    3. All right. Could you please write something about it, send me by email (my email is on top of the scripts I share here like this one https://raw.githubusercontent.com/freddenis/oracle-scripts/master/rac-status.sh) and I'll add it in a kind of 1.5 section.

      Thanks

      Delete

CUDA: Getting started on Google Colab

While getting started with CUDA on Windows or on WSL (same on Linux) requires to install some stuff, it is not the case when using Google...