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.
0
0
awk field extraction in scripts in Bash Scripting
Introduction
You want to get the second word from each line in a list of names.
You have a file with data separated by spaces or commas and want to grab a specific column.
You want to quickly check the size or date from a list of files shown by a command.
You want to extract the username from a system user list.
You want to process log files and get only the IP addresses.
Syntax
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.
Examples
Prints the first word of each line from file.txt.
Bash Scripting
awk '{ print $1 }' file.txtPrints the third field from each line, useful if fields are separated by spaces or tabs.
Bash Scripting
awk '{ print $3 }' data.csvUses comma as the field separator and prints the second field from each line.
Bash Scripting
awk -F, '{ print $2 }' data.csvSample Program
This 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.
Bash Scripting
#!/bin/bash # Extract the username (first field) from /etc/passwd awk -F: '{ print $1 }' /etc/passwd
OutputSuccess
Important Notes
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.
Summary
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.