gawk

text processingLinux/Unix
The gawk command is one of the most frequently used commands in Linux/Unix-like operating systems. gawk Pattern scanning and processing language - GNU version

Quick Reference

Command Name:

gawk

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

gawk [options] [arguments]

Common Use Cases

    Syntax

    gawk [options] -f program-file [file...]

    Options

    Option Description
    -F fs Use fs as the input field separator
    -f program-file Read the AWK program source from the file program-file
    -v var=val Assign the value val to the variable var, before program execution begins
    -W option Specify implementation-specific options (deprecated, use long options instead)
    --field-separator=fs Same as -F fs
    --file=program-file Same as -f program-file
    --assign=var=val Same as -v var=val
    --lint[=fatal] Warn about dubious or non-portable constructs
    --posix Operate in strict POSIX mode
    --profile[=file] Output profiling information to file or stderr
    --no-lines Don't generate #line directives in program
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the gawk command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    gawk '{print $1}' file.txt
    Print the first field of each line in file.txt.
    gawk -F: '{print $1}' /etc/passwd
    Print the first field of each line in /etc/passwd, using ':' as the field separator.
    echo "Hello World" | gawk '{print $2, $1}'
    Pipe output to gawk and print fields in reverse order. # Advanced Examples Advanced gawk 'BEGIN {sum=0} {sum+=$1} END {print "Sum:", sum}' numbers.txt Calculate the sum of the first field in each line. gawk '{gsub(/old/, "new"); print}' file.txt Replace all occurrences of "old" with "new" in each line. gawk '!seen[$0]++' file.txt Remove duplicate lines without sorting the file. gawk 'NR % 2 == 0' file.txt Print only even-numbered lines. gawk 'length($0) > 80' file.txt Print only lines longer than 80 characters.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    gawk (GNU awk) is an extended version of the standard awk text processing language, providing a powerful way to process and analyze text files. Key features of gawk: 1. Pattern-Action Processing: gawk processes input line by line, executing actions when patterns match, making it ideal for text transformation tasks. 2. Field Processing: It automatically splits input lines into fields, making it easy to extract and manipulate specific columns of data. 3. Programmable: gawk includes variables, functions, loops, conditionals, and arrays, offering a complete programming language for text processing. 4. Regular Expression Support: It provides powerful pattern matching capabilities through POSIX Extended Regular Expressions. 5. Built-in Functions: gawk includes numerous built-in functions for string manipulation, mathematics, and I/O operations. 6. BEGIN/END Blocks: Special blocks that execute before and after processing input, useful for initialization and summary operations. 7. Multiple Input Files: gawk can process multiple input files in sequence, treating them as a single continuous input stream. 8. Advanced Features: GNU awk extends the basic awk language with additional features like network I/O, dynamic regular expressions, and more sophisticated array handling. Common use cases for gawk include text extraction and transformation, data processing, report generation, log file analysis, and as a quick scripting tool for one-liners that would be more complex in other languages.

    Related Commands

    These commands are frequently used alongside gawk or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the gawk command works in different scenarios.

    $ gawk
    View All Commands