I want to talking about the “awk” command running as a grep or sed .This command is a  pattern scanning and processing, if you can use it as correct while working that contain complex  file, so it can be usefull.Also you can use regular expression while using awk ,  if you need of course.But i recommended to you got to use regex with awk.

Let’s start the review this command step by step to understand whic how to work this.

This command that prints first column in ugur.txt.

[root@rhel6 ~]# awk ‘{print $1}’ ugur.txt
linux
LINUX
ugur
hacker
redhat
rhel
rhellinux
bash
init
OS
architecture

This command that prints first and second columns in ugur.txt.

[root@rhel6 ~]# awk ‘{print $1,$2}’ ugur.txt
linux 0
LINUX 1
ugur 2
hacker 3
redhat 4
rhel 5
rhellinux 6
bash 7
init 8
OS 9
architecture 10

Prints first and second columns then adding the special character between first and second columns in ugur.txt.

[root@rhel6 ~]# awk ‘{print $1,”=>”,$2}’ ugur.txt
linux => 0
LINUX => 1
ugur => 2
hacker => 3
redhat => 4
rhel => 5
rhellinux => 6
bash => 7
init => 8
OS => 9
architecture => 10

Prints second column, if it has contain “1″.

[root@rhel6 ~]# awk '{if ($2 ~/1/) print $0}' ugur.txt
LINUX  1
architecture 10

Prints all columns that beginning as ‘ugur’.

[root@rhel6 ~]# awk ‘{if ($1 ~/ugur/) print $0,”=>”,$1}’ ugur.txt
ugur 2 => ugur

Prints all columns that contain as ‘linux’.

[root@rhel6 ~]# awk '/linux/ {print $1,"-",$0}' ugur.txt
linux - linux  0
rhellinux - rhellinux 6

Prints  first and  second columns with special character where line includes ‘ linux’.

[root@rhel6 ~]# awk ‘/^linux/ {print $1,”-”,$0}’ ugur.txt linux – linux  0

Prints line ending as ’7′.

[root@rhel6 ~]# grep 'bash' ugur.txt | awk '/7$/ {print $1}'
bash

Prints all processes (User,PID,Command Path)  where include first,second and third columns.

[root@rhel6 ~]#ps -auxx | awk ‘{print $1,”:”,$2,”:”,$11}’ | more
USER : PID : COMMAND
root : 1 : /sbin/init
root : 2 : [kthreadd]
root : 3 : [migration/0]
root : 4 : [ksoftirqd/0]
root : 5 : [migration/0]
root : 6 : [watchdog/0]
root : 7 : [events/0]
root : 8 : [cpuset]
root : 9 : [khelper]
root : 10 : [netns]

Prints process of postfix where include first,second and third columns.

[root@rhel6 ~]# ps -auxx | awk '/postfix/ {print $1,":",$2,":",$11}'
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root : 1674 : /usr/libexec/postfix/master
postfix : 1684 : qmgr
postfix : 2661 : pickup

According to the first five lines the first columns prints which contain ‘kernel’ then checking this command with ‘$1′ .

[root@rhel6 ~]# awk ‘{if ($5 ~/kernel/) print $0}’ /var/log/messages |tail -n 5 && echo ‘WORKED!’:$?
Jun 10 16:34:05 rhel6 kernel: cnic: Broadcom NetXtreme II CNIC Driver cnic v2.2.14 (Mar 30, 2011)
Jun 10 16:34:06 rhel6 kernel: bnx2fc: Broadcom NetXtreme II FCoE Driver bnx2fc v1.0.2 (Mar 28, 2011) Jun 10 16:34:09 rhel6 kernel: RPC: Registered udp transport module. Jun 10 16:34:09 rhel6 kernel: RPC: Registered tcp transport module. Jun 10 16:34:09
rhel6 kernel: RPC: Registered tcp NFSv4.1 backchannel transport module.

WORKED!:0

Written by Uğur Engin
Share on Facebook

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>