Skip to content

Latest commit

 

History

History
441 lines (301 loc) · 11.2 KB

awk.org

File metadata and controls

441 lines (301 loc) · 11.2 KB

Drills

Local variables

How to declair local variables in a function?

Answer

Local variables are declaired in the function arguments.

function f(      i){
    for (i = 0; i < 3; i++)
        print "bar's i=" i
}
function g(      i) { print i }
END { f(); g() }

Current filename

How do you get the current file name?

:in-file ./text-files/access.log

Answer

END { print FILENAME }

Current file row number

How to get the current file row number?

:in-file ./text-files/access.log

Answer

{ print FNR }

Number of fields in record

How to get the number of fields in the current record?

:in-file ./text-files/access.log

Answer

NF is the variable.

Example; print number of fields on each record.

{ print NF }

Read records variable

What is the internal variable used to tell how many records have been read?

:in-file ./text-files/access.log

Answer

{ print NR }

Empty line pattern

How to skip empty lines?

:in-file ./text-files/ledger.ledger

Answer

NF

Print line 7 to 10 of file

How do you print line 7 to 10 with a range pattern?

:in-file ./text-files/ledger.ledger

Answer

FNR==7,FNR==10

Skip lines starting with number

How do you skip a line that starts with a number?

:in-file ./text-files/ledger.ledger

Answer

$0 ~ /^[^0-9]/

How many requests have each IP address done

Count the number of requests that each unique IP address have done.

:in-file ./text-files/access.log

Answer

The answer also contains some sorting.

{ counts[$1]++ }
END { for (c in counts) { print c, counts[c] | "sort -r -k 2,2" }}

For loop

Write a for loop that print value 1 to 10

Answer

END { for (i=1; i <= 10; i++) { print i }}

The Structure of an AWK Program

Name the two most basic parts of an awk program.

Answer

pattern { action }

First fields equals to

How to filter rows where first field is “129.204.106.181”?

:in-file ./text-files/access.log

Answer

$1=="129.204.106.181"

Replace : with |

How do you replace all : with | in passwd file

:in-file ./text-files/passwd

Answer

BEGIN {FS=":"; OFS="|"}
      { print $1, $2, $3, $4, $5, $6, $7}

Zero or more regex

What is the regex symbol for zero or more?

Answer

*

One or more regex

What is the regex symbol for one or more?

Answer

+

Zero or one regex

What is the regex symbol for zero or one?

Answer

?

Beginning of line regex

What is the regex symbol for beginning of line?

Answer

^

End of line regex

What is the regex symbol for end of line?

Answer

$

Comments

How do you declair comments?

Answer

# This line is a comments
{ print }   # From here to end of line is a comment

Trim

Write a trim function that removes prefix and tailing whitespaces from $0. trim should be a function that takes one argument and return a new string. The substtuted string should then be printed.

Answer

function trim(s){ gsub(/^[ \t]+|[ \t]+$/, "", s); return s }
{ print trim($0) }

First position

What is the first position number in vectors and strings?

Answer

AWK is differente from anthor languages in the sensa that collections and string start with position 1 and not 0 as in many other languages.

1

Here is a showcase.

BEGIN { print index("abcdef", "a")}

Substring

How to print “abc” in string “abcdef” using substr.

Answer

BEGIN { print substr("abcdef", 1, 3) }

Sum population in Europe

Sum the population in Europe. Population is in field $3.

:in-file ./text-files/countries.txt

Answer

$4 ~ /Europe/ { sum+= $3 }
END { print "Population in Europe is", sum}

Multiple occurences

How do you get the SECOND certificate?

:in-file ./text-files/certificates

Answer

/BEGIN CERTIFICATE/ && ++c==2,/END CERTIFICATE/

Multiple occurences 2

How do you get the SECOND and THIRD certificate?

:in-file ./text-files/certificates

Answer

/BEGIN CERTIFICATE/ && (++c==2 || c==3),/END CERTIFICATE/

Ranges

Print only the parameters to the Java commands and nothing more.

:in-file ./text-files/text.yml

Answer

$0 ~ /.*-\sjava/,$0 ~ /.*config/
$0 ~ /command:/,$1 ~ /^[a-z]/ && ++c==2

Field separator

How do you change the field separator when invoking awk?

Answer

awk -F',' '{ print $1 }'

Last occurrence

wlp0s20f3  /  monthly

       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      2023-04     10,50 GiB |    2,02 GiB |   12,53 GiB |   41,51 kbit/s
      2023-05      8,15 GiB |    1,76 GiB |    9,91 GiB |   86,03 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated     22,05 GiB |    4,77 GiB |   26,82 GiB |
$1 ~ /^20/ {last=$0} END { print last }