BASH

Processing Text Data with awk for Columnar Output

Process and reformat text or log files column-wise using 'awk' in Bash, extracting specific fields and manipulating data efficiently for reports or parsing.

#!/bin/bash

# Create a sample data file
cat > data.txt << EOF
ID,Name,Email,Status
1,Alice,[email protected],Active
2,Bob,[email protected],Inactive
3,Charlie,[email protected],Active
EOF

echo "Original data.txt:"
cat data.txt
echo "
"

echo "Extracting Name and Email (skipping header):"
# Use awk to process the file
# -F',' sets the field separator to a comma
# NR > 1 skips the first line (header)
# print $2, $3 prints the second and third fields
awk -F',' 'NR > 1 { print $2 ", " $3 }' data.txt
echo "
"

echo "Filtering active users and reformatting:"
# Filter by Status (4th field) and reformat output
awk -F',' 'NR > 1 && $4 == "Active" { print "User: " $2 " (ID: " $1 ") is " $4 }' data.txt

# Clean up sample file
rm data.txt
How it works: This script demonstrates the power of `awk` for processing text data, particularly columnar or delimited files. It sets the field separator using `-F` and then uses `NR` (number of records/lines) to skip the header. The first `awk` command extracts and prints the 'Name' and 'Email' fields. The second example filters lines where the 'Status' field is 'Active' and then reformats the output into a more readable string. `awk` is exceptionally useful for quick data extraction, filtering, and reformatting without needing complex loops or regular expressions in Bash alone.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs