Skip to content

awk COMMAND

July 24, 2010

awk COMMAND:
awk command is used to manipulate the text.This command checks each line of a file, looking for patterns that match those given on the command line.

SYNTAX:
The Syntax is
awk ‘{pattern + action}’ {filenames}

OPTIONS:

-W version Display version information and exit.
-F Print help message and exit.

EXAMPLE:
Lets create a file file1.txt and let it have the following data:

Data in file1.txt
14 15 16
15 15 11
5 56 6
5 25 1
  1. To print the second column data in file1.txt
    awk '{print $2}' file1.txt

    This command will manipulate and print second column of text file (file1.txt). The output will look like

    15
    15
    56
    25

  2. To multiply the column-1 and column-2 and redirect the output to file2.txt:
    awk '{print $1,$2,$1*$2}' file1.txt > file2.txt
    Command Explanation:
    $1 : Prints 1st column
    $2 : Prints 2ndcolumn
    $1*$2 : Prints Result of $1 x $2
    file1.txt : input file
    > : redirection symbol
    file2.txt : output file

    The above command will redirect the output to file2.txt and it will look like,
    14 15 210
    15 15 225
    5 56 280
    5 25 12

Advertisements

From → Unix

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: