Three Ways to Invoke AWK
The most common way to run AWK is to pass the program as a single quoted command-line argument followed by one or more input files, for example awk '{ print $1 }' data.txt. If no file is named, AWK reads from standard input, which makes it a natural link in a pipeline. When your program grows past a couple of lines, you move it into a separate file and run it with awk -f program.awk data.txt. For a reusable tool, you add a shebang line so the script runs directly as an executable.
Cricket analogy: Choosing between an inline one-liner and a -f file is like a captain deciding between a quick single off one ball and setting up a full over strategy; each fits a different situation.
Quoting and Passing Variables
Always wrap inline AWK programs in single quotes so the shell does not expand $1, $2, or other dollar-sign fields as shell variables. If you need to inject a shell value into the program, prefer the -v option, as in awk -v threshold=100 '$3 > threshold', which sets the AWK variable before the input is read. Avoid splicing shell variables directly into the quoted program because it invites quoting bugs and, with untrusted data, code injection. The -v assignments happen before the BEGIN block runs.
Cricket analogy: Passing a value with -v instead of splicing it into the quotes is like handing the batsman a clear signal from the dugout rather than shouting across a noisy stadium where the message garbles.
Self-Executing Scripts
To make an AWK program runnable like any other command, put a shebang line at the top pointing to awk with the -f flag, mark the file executable with chmod +x, and place it on your PATH. The line #!/usr/bin/awk -f tells the kernel to run the rest of the file as an AWK program. Using /usr/bin/env awk -f is often more portable because it finds awk wherever it lives. Inside such a script you can use BEGIN and END blocks just as in an inline program.
Cricket analogy: A shebang turning a text file into a command is like a debutant getting a Test cap: the same player, but now officially fielded and called upon directly by name.
# 1. Inline one-liner reading from a file
awk '{ print $1 }' data.txt
# 2. From standard input in a pipeline
cat access.log | awk '{ print $1 }'
# 3. Program in a file
awk -f report.awk data.txt
# 4. Pass a value in cleanly with -v
awk -v threshold=100 '$3 > threshold { print $0 }' sales.txt
# 5. Self-executing script
cat > top.awk <<'EOF'
#!/usr/bin/env awk -f
{ print $1 }
EOF
chmod +x top.awk
./top.awk data.txtDo not wrap an inline AWK program in double quotes when it contains $1, $2, etc. The shell will try to expand those as shell variables (usually to empty strings), silently breaking your program. Use single quotes, and pass in external values with -v.
- Run inline programs as awk 'program' file, or omit the file to read standard input in a pipeline.
- For larger programs, put the code in a file and run it with awk -f program.awk.
- Wrap inline programs in single quotes so the shell does not expand $1, $2, and friends.
- Pass external values with -v name=value, which is set before input is read.
- Add a shebang (#!/usr/bin/env awk -f) plus chmod +x to make a self-executing script.
- BEGIN and END blocks work the same whether inline or in a file.
Practice what you learned
1. Why should inline AWK programs be wrapped in single quotes?
2. What is the recommended way to pass a shell value into an AWK program?
3. How do you run an AWK program stored in a file named report.awk?
4. What happens if you run awk '{ print $1 }' with no filename argument?
5. Which shebang line is generally the most portable for an AWK script?
Was this page helpful?
You May Also Like
What Is AWK?
AWK is a compact, data-driven programming language built for scanning text files line by line and reacting to patterns. It excels at extracting columns, computing summaries, and reshaping structured text.
Your First AWK One-Liner
Walk through building practical AWK one-liners step by step: printing columns, filtering rows, summing fields, and counting lines. These small programs cover a huge share of everyday text tasks.
AWK Pattern-Action Syntax
Every AWK program is a sequence of pattern { action } rules. Understanding how patterns select lines and actions transform them is the core mental model of the whole language.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics