AWK: Difference between revisions

1,125 bytes added ,  4 years ago
m (Protected "AWK" ([Edit=Allow only logged in users] (indefinite) [Move=Allow only logged in users] (indefinite) [Delete=Allow only logged in users] (indefinite)))
 
(8 intermediate revisions by the same user not shown)
Line 1:
[[Category:Linux]]
__TOC__
<br />
 
= Basics =
 
Source: [https://www.geeksforgeeks.org/awk-command-unixlinux-examples/ geeksforgeeks.org]
cat stats_collection.log | grep svmem | awk '{print $1 $2," - ",$7}'
 
* Awk is a scripting language used for manipulating data and generating reports.
* Field variables — $1, $2, $3,
* $0 is the entire line.
* Usage:
cat stats_collection.log | grep svmem | awk '{print $1 " " $2," - ",$7}'
 
= Built In Variables =
 
* 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;}' employee.txt
 
* Print lines with more than 10 characters:
awk 'length($0) > 10' employee.txt
 
 
<br />
;References
<references/>
<br />
<br />
<br />
 
 
{{DISQUS}}