Awk helps you pick out parts of text lines easily. It splits lines into pieces called fields, so you can get just what you want.
awk field extraction in scripts in Bash Scripting
awk '{ print $n }' filename # n is the field number you want to extract
Fields are numbered starting at 1, so $1 is the first field, $2 the second, and so on.
By default, awk splits fields by spaces or tabs.
awk '{ print $1 }' file.txtawk '{ print $3 }' data.csvawk -F, '{ print $2 }' data.csvThis script uses awk to get the first field from each line in the /etc/passwd file, which is the username. The colon ':' is used as the field separator.
#!/bin/bash # Extract the username (first field) from /etc/passwd awk -F: '{ print $1 }' /etc/passwd
You can change the field separator with the -F option if your data uses commas, colons, or other characters.
Awk works line by line, so it's great for processing files or command outputs.
Remember fields start at 1, not 0 like some programming languages.
Awk splits lines into fields and lets you pick any field easily.
Use $1, $2, etc., to get the first, second, and other fields.
Change the field separator with -F if needed.
Practice
awk '{print $2}' file.txt do in a bash script?Solution
Step 1: Understand awk field numbering
Awk splits each line into fields separated by spaces by default. $1 is the first field, $2 is the second, and so on.Step 2: Analyze the command
The command prints $2, which is the second field of each line in file.txt.Final Answer:
Prints the second word (field) from each line of file.txt -> Option AQuick Check:
Awk $2 prints second field [OK]
- Confusing $2 with line number
- Thinking it prints entire line
- Mixing up field numbers starting at 0
Solution
Step 1: Identify field separator option
The -F option sets the field separator. For comma-separated files, use -F ','.Step 2: Check print syntax
The correct print syntax is '{print $3}' to print the third field.Final Answer:
awk -F ',' '{print $3}' file.csv -> Option AQuick Check:
Use -F ',' before script block [OK]
- Placing -F inside the print statement
- Using wrong separator like semicolon for CSV
- Misplacing quotes around separator
data.txt with the following content:apple 10 red banana 5 yellow cherry 20 red
What is the output of
awk '{print $1, $3}' data.txt?Solution
Step 1: Identify fields in each line
Each line has three fields: $1=fruit, $2=number, $3=color.Step 2: Print first and third fields
The command prints $1 and $3 separated by space, so output lines are fruit and color pairs.Final Answer:
apple red banana yellow cherry red -> Option BQuick Check:
Print $1 and $3 fields [OK]
- Printing wrong fields like $2 instead of $3
- Confusing field order
- Expecting numbers instead of colors
awk '{print $1}' -F ':' file.txtSolution
Step 1: Check placement of -F option
The -F option must come before the script block in awk commands.Step 2: Analyze the command structure
Correct syntax is: awk -F ':' '{print $1}' file.txtFinal Answer:
The -F option should come before the script block -> Option CQuick Check:
-F before script block [OK]
- Placing -F after the script block
- Using wrong field variable
- Misordering file name and script
records.txt with lines like:John|Doe|30|Engineer Jane|Smith|25|Designer Bob|Brown|40|Manager
Write an awk command to print only the names (first and last) of people older than 28.
Solution
Step 1: Set correct field separator
The file uses '|' as separator, so use -F '|' to split fields correctly.Step 2: Filter by age and print names
Use condition $3 > 28 to select lines where age is greater than 28, then print first and second fields (first and last names).Final Answer:
awk -F '|' '$3 > 28 {print $1, $2}' records.txt -> Option DQuick Check:
Filter $3 > 28 and print $1, $2 [OK]
- Using wrong field separator
- Using wrong comparison operator
- Not filtering before printing
Ready to prove it?
Pass the test and this pattern moves unknown
