0
0
Linux CLIscripting~10 mins

awk patterns and actions in Linux CLI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the first field of each line using awk.

Linux CLI
awk '{ print [1] }' file.txt
Drag options to blanks, or click blank then click option'
A1
BNF
C$1
D$0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of $1 prints the number 1 literally.
Using $0 prints the whole line, not just the first field.
2fill in blank
medium

Complete the code to print lines where the second field equals 'error'.

Linux CLI
awk '$2 == [1] { print $0 }' logfile.txt
Drag options to blanks, or click blank then click option'
A"error"
Berror"
C'error'
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causes awk to treat error as a variable.
Using single quotes inside the awk script breaks syntax.
3fill in blank
hard

Fix the error in the awk command to print lines where the third field is greater than 100.

Linux CLI
awk '$3 [1] 100 { print $0 }' data.txt
Drag options to blanks, or click blank then click option'
A==
B<
C=>
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using => causes syntax error in awk.
Using == checks for equality, not greater than.
4fill in blank
hard

Fill both blanks to print the first and third fields separated by a space.

Linux CLI
awk '{ print [1], [2] }' file.csv
Drag options to blanks, or click blank then click option'
A$1
B$2
C$3
D"-"
Attempts:
3 left
💡 Hint
Common Mistakes
Using $2 instead of $3 prints the wrong field.
Using quotes instead of field variables prints literal text.
5fill in blank
hard

Fill all three blanks to create a dictionary of words and their lengths for words longer than 3 characters.

Linux CLI
awk '{ if (length([1]) [2] 3) dict[[3]] = length($1) } END { for (w in dict) print w, dict[w] }' words.txt
Drag options to blanks, or click blank then click option'
A$1
B>
Cw
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using w before it is defined causes errors.
Using length as a key is incorrect.
Using < instead of > changes the condition.