How to declair local variables in a function?
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() }
How do you get the current file name?
:in-file ./text-files/access.log
END { print FILENAME }
How to get the current file row number?
:in-file ./text-files/access.log
{ print FNR }
How to get the number of fields in the current record?
:in-file ./text-files/access.log
NF is the variable.
Example; print number of fields on each record.
{ print NF }
What is the internal variable used to tell how many records have been read?
:in-file ./text-files/access.log
{ print NR }
How to skip empty lines?
:in-file ./text-files/ledger.ledger
NF
How do you print line 7 to 10 with a range pattern?
:in-file ./text-files/ledger.ledger
FNR==7,FNR==10
How do you skip a line that starts with a number?
:in-file ./text-files/ledger.ledger
$0 ~ /^[^0-9]/
Count the number of requests that each unique IP address have done.
:in-file ./text-files/access.log
The answer also contains some sorting.
{ counts[$1]++ }
END { for (c in counts) { print c, counts[c] | "sort -r -k 2,2" }}
Write a for loop that print value 1 to 10
END { for (i=1; i <= 10; i++) { print i }}
Name the two most basic parts of an awk program.
pattern { action }
How to filter rows where first field is “129.204.106.181”?
:in-file ./text-files/access.log
$1=="129.204.106.181"
How do you replace all : with | in passwd file
:in-file ./text-files/passwd
BEGIN {FS=":"; OFS="|"}
{ print $1, $2, $3, $4, $5, $6, $7}
What is the regex symbol for zero or more?
*
What is the regex symbol for one or more?
+
What is the regex symbol for zero or one?
?
What is the regex symbol for beginning of line?
^
What is the regex symbol for end of line?
$
How do you declair comments?
# This line is a comments
{ print } # From here to end of line is a comment
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.
function trim(s){ gsub(/^[ \t]+|[ \t]+$/, "", s); return s }
{ print trim($0) }
What is the first position number in vectors and strings?
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")}
How to print “abc” in string “abcdef” using substr
.
BEGIN { print substr("abcdef", 1, 3) }
Sum the population in Europe. Population is in field $3.
:in-file ./text-files/countries.txt
$4 ~ /Europe/ { sum+= $3 }
END { print "Population in Europe is", sum}
How do you get the SECOND certificate?
:in-file ./text-files/certificates
/BEGIN CERTIFICATE/ && ++c==2,/END CERTIFICATE/
How do you get the SECOND and THIRD certificate?
:in-file ./text-files/certificates
/BEGIN CERTIFICATE/ && (++c==2 || c==3),/END CERTIFICATE/
Print only the parameters to the Java commands and nothing more.
:in-file ./text-files/text.yml
$0 ~ /.*-\sjava/,$0 ~ /.*config/
$0 ~ /command:/,$1 ~ /^[a-z]/ && ++c==2
How do you change the field separator when invoking awk
?
awk -F',' '{ print $1 }'
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 }