0% found this document useful (0 votes)
27 views5 pages

Awk

The Awk command is a powerful tool used for text processing in Unix and Linux systems. It allows users to search for patterns in files, manipulate data, and generate reports. Awk programs consist of patterns and actions, where patterns are expressions or regex matches against input lines and actions specify what to do for a matching pattern.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Awk

The Awk command is a powerful tool used for text processing in Unix and Linux systems. It allows users to search for patterns in files, manipulate data, and generate reports. Awk programs consist of patterns and actions, where patterns are expressions or regex matches against input lines and actions specify what to do for a matching pattern.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Awk

The Awk command is a powerful tool used for text processing in Unix and
Linux systems. It allows users to search for patterns in files, manipulate
data, and generate reports. With its versatile functionality, Awk is
commonly used for tasks such as extracting specific columns from a file,
performing mathematical operations, and automating repetitive tasks. Its
simple syntax and powerful capabilities make it a favorite among system
administrators and programmers alike.

Awk = Aho Weinberger and Kernighan

Version POSIX awk called awk

Awk command line

awk [-F ifs] [-f prog_file] … [-v var=val]… [argument]…

1. [-F ifs]: This option sets the field separator (FS) used by AWK to
split input lines into fields. Here:

-F indicates that what follows (ifs) is the field separator.

ifs is the input field separator, which can be a single character


or a regular expression. By default, AWK uses whitespace (spaces
and tabs) as the field separator.

2. [-f prog_file]: This option specifies a file (prog_file) containing the


AWK program to be executed. The AWK program is a set of instructions
written in the AWK scripting language.

3. [-v var=val]: This option allows you to declare a variable (var) and
assign it a value (val) that can be accessed within the AWK program.
For example, -v myvar=10 would declare a variable myvar with the value
10 in AWK.

4. [argument]: This represents the input data or file(s) that AWK will
process. If no input is specified, AWK reads from standard input
(usually what is piped or redirected into it).

Exemple;

grades.txt

Alice 85

Bob 72

Charlie 90

David 68
Eve 95

bash

awk '$2 >= 80 { print $1 }' grades.txt

output

Alice

Charlie

Eve

$1 → first field

$2 → second field

awk -F " " '$1 == "Alice" { print $1 " has " $2 }' grades.txt

Alice has 85

general structure of an AWK program:

pattern1 { action1 }

pattern2 { action2 }

pattern3 { action3 }

...

Patterns

Patterns can be:

Expressions: evaluates to true or false based on the current input


line.

Regular expressions: matches against the current input line.

Special patterns: such as BEGIN (executes before processing any input)


and END (executes after processing all input),.

Actions

Actions specify what AWK should do when a pattern is true. Actions can
include:
Commands: print statements, variable assignments, arithmetic
operations, etc.

Control statements: if, else, for, while, etc., for more complex logic.

Built-in functions: AWK provides many built-in functions for string


manipulation, arithmetic, formatting output, etc.

Record → string separated by \n = a line .

Filed → string separated by "" (or by a character specified by -F) = a word

A line ( records) is formed by words ( fields).

$0: represents the entire input line.

NF: represents the number of fields (or columns) in the current input
line.

$NF: represents the value of the last field (or column) in the current
input line.

For example, if you have a line of input like Alice 25 30 35:

$0 would be "Alice 25 30 35" (the entire line).

NF would be 4 (since there are four fields: Alice, 25, 30, 35).

$NF would be 35 (the value of the last field in the line).

awk {print} file.txt = cat file.txt

1. Using Command-Line Input


Method:

Directly provide the AWK program as a string argument to the awk command.

Example:

```bash

# Print the first field of each line in data.txt

awk '{ print $1 }' data.txt

2. Using AWK Script File


Method:

Write your AWK program in a separate file and execute it using the -f
option with awk.

Example:

Create a file named print_second_field.awk with the following content:

```awk

# print_second_field.awk

{ print $2 }

Execute the script in data.txt:

```bash

awk -f print_second_field.awk data.txt

3. Using Inline Input


Method:

Pipe input directly into the awk command using echo or other commands.

Example:

```bash

# Print the second field of a specific input line

echo "Alice 25" | awk '{ print $2 }'

4. Using Here Document


Method:

Use a here document (`<<`) to provide multi-line input to awk


interactively.

Example:

```bash

# Calculate and print the length of each line provided as input

awk '{ print length($0) }' << END

Hello

World

END
awk '{print "hello king ! "}'

→ print until you enter ^c

You might also like