📄 Sample Input File (Named: data.txt)
🧠 AWK Command Tutorial with Sample Inputs
Begin
The BEGIN block in awk is executed before any line of the input file is processed. It's perfect for initializing headers, setting variables, and formatting output.
✅ Recap: Basic BEGIN Block
awk 'BEGIN {print "Name Score Subject"} {print}' data.txt
Output:
🎯 FAANG-Style Scenario-Based Questions Using BEGIN
🔹 Q1: Print a report with a title, current timestamp, and then the data
🧠 Why?
BEGIN helps you insert metadata (like date/time) before processing file content.
...
🔹 Q2: Set a custom field separator before processing a CSV file
Input (students.csv):
🧠 Why?
Use BEGIN to define FS (Field Separator) and OFS (Output Field Separator) before data is parsed.
Output:
🔹 Q3: Print average marks, but initialize sum/count in BEGIN
🧠 Why?
BEGIN sets up initial values (sum=0, count=0) to avoid garbage memory or side effects.
Output:
Average Marks: 78
🔹 Q4: Print a table with headers, aligned columns using printf
🧠 Why?
BEGIN is great for printing headers and formatting once, while the main block prints rows.
Output:
...
🔹 Q5: Initialize a counter or hash map in BEGIN block
🧠 Why?
Pre-load categories or counters in BEGIN for controlled processing or initializing associative arrays.
Output:
💡 BONUS Tips to Master BEGIN
📘 What Are System Variables in AWK?
System (built-in) variables in awk give you metadata about each record/line, fields, files, formatting, and processing status. You can use them in any block: BEGIN, pattern/action, or END.
📊 Table of Popular awk System Variables
🔍 Example Input File: data.txt
🧪 Practical Examples of System Variables
🔹 NR (Record Number)
📌 Output:
1 Alice 80 Math
2 Bob 70 English
...
🔹 NF (Number of Fields)
📌 Useful to detect malformed lines or extra fields.
🔹 FS and OFS (Field Separators)
If file.csv:
Alice,80
Bob,70
📌 Output:
Alice => 80
Bob => 70
🔹 FILENAME
📌 Output:
data.txt Alice 80 Math
...
🔹 RS (Record Separator)
Input: paragraph.txt
This is para one.
This is para two.
📌 Treats blank line as paragraph separator.
🔹 ENVIRON
📌 Outputs shell environment variable from within awk.
🔹 ARGV & ARGC
📌 Lists input file and any command-line arguments passed.
🔹 OFMT and CONVFMT (Format floats)
📌 Output:
3.14
🎓 FAANG-Ready Questions Using System Variables
Q1: How do you detect lines with fewer than expected fields?
🧠 Checks malformed lines (e.g., corrupted data file).
Q2: Print line number, filename, and number of fields.
Q3: How to parse environment variables in awk?
Q4: Convert input records by paragraphs instead of lines.
🧠 Hints and Memory Tricks
Would you like a Notion-based Flashcard Deck or a Cheat Sheet PDF for these system variables?