0
0
Bash Scriptingscripting~10 mins

awk field extraction in scripts in Bash Scripting - 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 second field of each line using awk.

Bash Scripting
awk '{ print $[1] }' file.txt
Drag options to blanks, or click blank then click option'
A2
B0
C1
D3
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.
2fill in blank
medium

Complete 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'
A1
B3
C0
D2
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.
3fill in blank
hard

Fix 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'
ANR
BNF
C0
D1
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.
4fill in blank
hard

Fill 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'
A2
B3
C4
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field numbers causes wrong output.
Forgetting quotes around the comma causes syntax errors.
5fill in blank
hard

Fill 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'
A1
B3
C2
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using $2 instead of $1 prints wrong fields.
Using 4 instead of 3 changes the filter condition.