📖 Scenario: You have a text file with information about employees. Each line contains the employee's ID, name, and department separated by spaces.You want to write a bash script that extracts specific fields from each line using awk.
🎯 Goal: Build a bash script that reads the employee data and extracts the employee names using awk.
📋 What You'll Learn
Create a text file variable with employee data
Create a variable for the field number to extract
Use awk to extract the specified field from each line
Print the extracted fields
💡 Why This Matters
🌍 Real World
Extracting specific columns from text data is common when processing logs, reports, or CSV files in automation scripts.
💼 Career
Knowing how to use awk in bash scripts helps automate data extraction tasks in system administration, data analysis, and DevOps roles.
Progress0 / 4 steps
1
Create the employee data variable
Create a variable called employee_data that contains these exact three lines of text, each line separated by a newline character: 101 Alice Sales 102 Bob Marketing 103 Charlie IT
Bash Scripting
Hint
Use double quotes and \n to separate lines inside the string.
2
Set the field number to extract
Create a variable called field_number and set it to 2 to represent the employee name field.
Bash Scripting
Hint
Just assign the number 2 to the variable field_number.
3
Extract the specified field using awk
Use echo to send employee_data to awk. Use awk with the field variable field_number to extract that field from each line. Store the result in a variable called extracted_names.
Bash Scripting
Hint
Use awk -v field="$field_number" '{print $field}' to print the chosen field.
4
Print the extracted names
Print the variable extracted_names to display the extracted employee names.
Bash Scripting
Hint
Use printf "%s\n" "$extracted_names" to print each name on its own line.
Practice
(1/5)
1. What does awk '{print $2}' file.txt do in a bash script?
easy
A. Prints the second word (field) from each line of file.txt
B. Prints the entire line from file.txt
C. Prints the first word (field) from each line of file.txt
D. Prints the number of lines in file.txt
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 A
Quick Check:
Awk $2 prints second field [OK]
Hint: Remember $1 is first field, $2 second, etc. [OK]
Common Mistakes:
Confusing $2 with line number
Thinking it prints entire line
Mixing up field numbers starting at 0
2. Which of the following is the correct syntax to print the third field using awk with a comma as the field separator?
easy
A. awk -F ',' '{print $3}' file.csv
B. awk '{print $3 -F ','}' file.csv
C. awk '{print $3, -F ","}' file.csv
D. awk -F ';' '{print $3}' file.csv
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 A
Quick Check:
Use -F ',' before script block [OK]
Hint: Use -F 'separator' before the awk script block [OK]
Common Mistakes:
Placing -F inside the print statement
Using wrong separator like semicolon for CSV
Misplacing quotes around separator
3. Given a file 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?
medium
A. apple 10
banana 5
cherry 20
B. apple red
banana yellow
cherry red
C. 10 red
5 yellow
20 red
D. apple banana cherry
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 B
Quick Check:
Print $1 and $3 fields [OK]
Hint: Match $1, $2, $3 to columns in data [OK]
Common Mistakes:
Printing wrong fields like $2 instead of $3
Confusing field order
Expecting numbers instead of colors
4. What is wrong with this awk command to print the first field from a colon-separated file? awk '{print $1}' -F ':' file.txt
medium
A. The colon separator is invalid in awk
B. The $1 should be $0 to print the whole line
C. The -F option should come before the script block
D. The file name should come before the script block
Solution
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.txt
Final Answer:
The -F option should come before the script block -> Option C