Awk: Difference between revisions

From miki
Jump to navigation Jump to search
Line 4: Line 4:
== Awk Program Examples ==
== Awk Program Examples ==
<source lang="bash">
<source lang="bash">
ps al | awk '{print $2}' # Print second field of ps output
ps al | awk '{print $2}' # Print second field of ps output
find /proc -type l | awk -F"/" '{print $3}' # Print second folder name (i.e. process pid)
arp -n 10.137.3.129|awk '/ether/{print $3}' # Print third field of arp output, if line contains 'ether' somewhere
getent hosts unix.stackexchange.com | awk '{ print $1 ; exit }' # Print only first line, then exit
find /proc -type l | awk -F"/" '{print $3}' # Print second folder name (i.e. process pid)
</source>
</source>



Revision as of 09:44, 4 December 2015

References

Awk Program Examples

ps al | awk '{print $2}'                                         # Print second field of ps output
arp -n 10.137.3.129|awk '/ether/{print $3}'                      # Print third field of arp output, if line contains 'ether' somewhere
getent hosts unix.stackexchange.com | awk '{ print $1 ; exit }'  # Print only first line, then exit
find /proc -type l | awk -F"/" '{print $3}'                      # Print second folder name (i.e. process pid)

Tips

  • Defining environment variable - Using an Awk script and Bash builtin eval
eval $(awk 'BEGIN{printf "MY_VAR=value";}')
echo $MY_VAR