awk Linux Commands
What is Linux awk Command?
Explanation
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 :
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 |
| 141516 |
| 151511 |
| 5566 |
| 5251 |
- 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
- 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 125