100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Running AWK Scripts

There are several ways to run AWK: inline one-liners, program files with -f, and self-executing scripts with a shebang. Knowing each lets you scale from a quick command to a maintainable tool.

FoundationsBeginner8 min readJul 10, 2026
Analogies

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.

bash
# 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.txt

Do 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

Was this page helpful?

Topics covered

#Programming#AWKStudyNotes#RunningAWKScripts#Running#AWK#Scripts#Three#StudyNotes#SkillVeris#ExamPrep