<title>Linux <i>awk</i> Usage</title> Linux
awkUsagetable of Contents
- 1. Sum a specific column:
- 2. Sum the columns of the lines with additional conditions (starting with 7):
- 3. Output the line whose value in the 3rd column is 1:
- 4. Output the line whose $ < $ > 20:
- 5. Count the lines starting with 7:
- 6. Output the line containg number 765
- 7. Output the line containing number 765 with the line number in front of this line:
- 8. Output the lines not containing number 765:
In this post, I am going to record the common patterns of using awk, theamazing tool for processing file.
Assume I has a file (BB) with the following contents:
70 23 2757 35 3765 56 3765 56 123 411 3
1Sum a specific column:
awk ' {sum + = ' {} END {print sum} ' bb |
2Sum the columns of the lines with additional conditions (starting with 7):
awk '/^7/{print $} ' bb | awk ' {sum + = $ '} END {print sum} '
3Output the line whose value in the 3rd column is 1:
awk ' $ = = 1 ' bb
4Output the line whose $ < $ > 20:
awk ' {$1<80 && $2>20;print} ' bb
5Count the lines starting with 7:
awk '/^7/{x + +;} END {print x} ' bb
6Output the line containg number 765
awk '/765/' bb
7Output the line containing number 765 with the line number in front of this line:
awk '/765/{print NR ': ' $} ' bb
In the above command, the NR are the built-in variable that represents the Linebeing processed, the $ refers to the whole line.9
8Output the lines not containing number 765:
awk '!/765/' bb
Author:wujing
CREATED:2015-01-23 Five 21:22
Emacs 24.3.1 (ORG mode 8.2.10)
Linux/awk/usage