Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In awk, $1 refers to the first field of the current line.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
String values in awk patterns must be enclosed in double quotes.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using => causes syntax error in awk.
Using == checks for equality, not greater than.
✗ Incorrect
The correct operator for 'greater than' is >, not =>.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $2 instead of $3 prints the wrong field.
Using quotes instead of field variables prints literal text.
✗ Incorrect
Use $1 for the first field and $3 for the third field to print them separated by a space.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Use $1 to get the word, > to check length, and $1 again as the dictionary key.