Awk: Difference between revisions

From www.ReeltoReel.nl Wiki
Jump to navigation Jump to search
(Created page with "=awk= pattern {action} pattern {action} $ awk '{print $2}' /etc/fstab $ awk '/^[^#]/ {print $2}' /etc/fstab to filter out comment lines - starting with # print first f...")
 
No edit summary
Line 6: Line 6:
  $ awk '{print $2}' /etc/fstab
  $ awk '{print $2}' /etc/fstab


to filter out comment lines - starting with #
  $ awk '/^[^#]/ {print $2}' /etc/fstab
  $ awk '/^[^#]/ {print $2}' /etc/fstab
to filter out comment lines - starting with #


print first field from password file using field seperator ':'
print first field from password file using field seperator ':'
Line 18: Line 18:
  $ awk '{ sum=0; for (i=1; i<=NF; i++) sum +=$i; print sum/NF; }'
  $ awk '{ sum=0; for (i=1; i<=NF; i++) sum +=$i; print sum/NF; }'


<pre>
{|
NF
|-
| NF || number of fields in the current line
|-
| NR || the current record number (line number)
|-
| FS || input field seperator (space by default)
|-
| RS || record seperator (newline by default)
|-
| Example || Example
|-
| Example || Example
|-
| Example || Example
|-
| Example || Example
|-
| Example || Example
|}

Revision as of 16:05, 9 October 2014

awk

pattern {action}
pattern {action}
$ awk '{print $2}' /etc/fstab

to filter out comment lines - starting with #

$ awk '/^[^#]/ {print $2}' /etc/fstab

print first field from password file using field seperator ':'

$ awk -F: '{print$1}' /etc/passwd

print only user ID > 500

$ awk -F: '$3>=500 {print$1}' /etc/passwd

display average of numbers entereed on a line:

$ awk '{ sum=0; for (i=1; i<=NF; i++) sum +=$i; print sum/NF; }'
NF number of fields in the current line
NR the current record number (line number)
FS input field seperator (space by default)
RS record seperator (newline by default)
Example Example
Example Example
Example Example
Example Example
Example Example