AWK: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
<br /> |
<br /> |
||
Usage: |
|||
cat stats_collection.log | grep svmem | awk '{print $1 $2," - ",$7}' |
cat stats_collection.log | grep svmem | awk '{print $1 $2," - ",$7}' |
||
= Built In Variables = |
|||
Awk’s built-in variables include the field variables—$1, $2, $3, |
* Awk’s built-in variables include the field variables—$1, $2, $3, |
||
* $0 is the entire line. |
|||
* NR: current count of the number of lines. |
|||
awk '{print NR,$0}' employee.txt |
|||
⚫ | |||
FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator. |
|||
RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline. |
|||
OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter. |
|||
ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print. |
|||
⚫ | |||
awk '{print $1,$NF}' employee.txt |
|||
awk 'NR==3, NR==6 {print NR,$0}' employee.txt => Display Line From 3 to 6 |
|||
awk 'END { print NR }' employee.txt => Count the lines in a file |
|||
* FS: Field separator character. |
|||
The default is “white space”, meaning space and tab characters. |
|||
FS can be reassigned to another character (typically in BEGIN) to change the field separator. |
|||
= Examples = |
|||
* To find/check for any string in any column: |
|||
awk '{ if($3 == "B6") print $0;}' geeksforgeeks.txt |
|||
* Print lines with more than 10 characters: |
|||
awk 'length($0) > 10' employee.txt |
|||
Revision as of 10:17, 13 April 2019
Usage:
cat stats_collection.log | grep svmem | awk '{print $1 $2," - ",$7}'
Built In Variables
- Awk’s built-in variables include the field variables—$1, $2, $3,
- $0 is the entire line.
- NR: current count of the number of lines.
awk '{print NR,$0}' employee.txt
- NF: count of the number of fields within the current input record.
awk '{print $1,$NF}' employee.txt awk 'NR==3, NR==6 {print NR,$0}' employee.txt => Display Line From 3 to 6 awk 'END { print NR }' employee.txt => Count the lines in a file
- FS: Field separator character.
The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator.
Examples
- To find/check for any string in any column:
awk '{ if($3 == "B6") print $0;}' geeksforgeeks.txt
- Print lines with more than 10 characters:
awk 'length($0) > 10' employee.txt
- References
{{#widget:DISQUS
|id=networkm
|uniqid=AWK
|url=https://aman.awiki.org/wiki/AWK
}}