Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the second field of each line using awk.
Bash Scripting
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 $2 prints the first field.
Using $0 prints the whole line, not a single field.
✗ Incorrect
The $2 in awk prints the second field of each line.
2fill in blank
mediumComplete the code to print the first and third fields separated by a dash.
Bash Scripting
awk '{ print $[1] "-" $3 }' data.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $2 instead of $1 prints the wrong field.
Forgetting to add quotes around the dash causes syntax errors.
✗ Incorrect
Using $1 and $3 prints the first and third fields. The dash is added as a string between them.
3fill in blank
hardFix the error in the code to print the last field of each line.
Bash Scripting
awk '{ print $[1] }' file.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using NR prints the line number, not a field.
Using $0 prints the whole line, not a single field.
✗ Incorrect
NF is a special awk variable that holds the number of fields in the current line, so $NF prints the last field.
4fill in blank
hardFill both blanks to print fields 2 and 4 separated by a comma.
Bash Scripting
awk '{ print $[1] "," $[2] }' input.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field numbers causes wrong output.
Forgetting quotes around the comma causes syntax errors.
✗ Incorrect
To print the second and fourth fields, use $2 and $4 separated by a comma string.
5fill in blank
hardFill all three blanks to create a dictionary of word lengths for words longer than 3 characters.
Bash Scripting
awk '{ if(length($[1]) > [2]) print $[3], length($[3]) }' words.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $2 instead of $1 prints wrong fields.
Using 4 instead of 3 changes the filter condition.
✗ Incorrect
We check the length of the first field ($1) to be greater than 3, then print the word ($1) and its length.