Skip to content
Tags

awkward right?

January 6, 2011

There my be times in which I will need certain columns of information instead of viewing all the columns that gets printed in the terminal. In the below example I only want to see columns 1, 3 & 8 I do not want to see all the columns: –

prompt> ls -l

total 96

drwxr-xr-x 2 root root 4096 2010-11-24 19:46 bin

drwxr-xr-x 3 root root 4096 2010-11-24 19:50 boot

lrwxrwxrwx 1 root root 11 2010-02-10 17:59 cdrom -> media/cdrom

drwxr-xr-x 18 root root 4120 2011-01-03 20:36 dev

drwxr-xr-x 144 root root 12288 2011-01-03 20:39 etc

drwxr-xr-x 3 root root 4096 2010-02-10 18:08 home

lrwxrwxrwx 1 root root 33 2010-10-08 19:57 initrd.img -> boot/initrd.img-2.6.32-25-generic

lrwxrwxrwx 1 root root 33 2010-07-31 15:34 initrd.img.old -> boot/initrd.img-2.6.32-24-generic

drwxr-xr-x 20 root root 16384 2010-11-24 19:46 lib

drwx—— 2 root root 16384 2010-02-10 17:59 lost+found

drwxr-xr-x 4 root root 4096 2011-01-03 14:25 media

drwxr-xr-x 2 root root 4096 2009-10-20 01:04 mnt

drwxr-xr-x 2 root root 4096 2009-10-28 20:55 opt

dr-xr-xr-x 143 root root 0 2011-01-03 20:36 proc

drwx—— 11 root root 4096 2010-12-03 17:31 root

drwxr-xr-x 2 root root 4096 2010-11-24 19:48 sbin

drwxr-xr-x 2 root root 4096 2009-10-20 00:05 selinux

drwxr-xr-x 2 root root 4096 2009-10-28 20:55 srv

drwxr-xr-x 12 root root 0 2011-01-03 20:36 sys

drwxrwxrwt 18 root root 4096 2011-01-03 20:41 tmp

drwxr-xr-x 10 root root 4096 2009-10-28 20:55 usr

drwxr-xr-x 15 root root 4096 2009-10-28 21:02 var

lrwxrwxrwx 1 root root 30 2010-10-08 19:57 vmlinuz -> boot/vmlinuz-2.6.32-25-generic

lrwxrwxrwx 1 root root 30 2010-07-31 15:34 vmlinuz.old -> boot/vmlinuz-2.6.32-24-generic

So to see just the columns I want to see and not see any other columns I can use the awk command, as below:-

prompt> ls -l | awk ‘{print “permission”,$1,”owner”,$3,”name=”$8}’

permission total owner name=

permission drwxr-xr-x owner root name=bin

permission drwxr-xr-x owner root name=boot

permission lrwxrwxrwx owner root name=cdrom

permission drwxr-xr-x owner root name=dev

permission drwxr-xr-x owner root name=etc

permission drwxr-xr-x owner root name=home

permission lrwxrwxrwx owner root name=initrd.img

permission lrwxrwxrwx owner root name=initrd.img.old

permission drwxr-xr-x owner root name=lib

permission drwx—— owner root name=lost+found

permission drwxr-xr-x owner root name=media

permission drwxr-xr-x owner root name=mnt

permission drwxr-xr-x owner root name=opt

permission dr-xr-xr-x owner root name=proc

permission drwx—— owner root name=root

permission drwxr-xr-x owner root name=sbin

permission drwxr-xr-x owner root name=selinux

permission drwxr-xr-x owner root name=srv

permission drwxr-xr-x owner root name=sys

permission drwxrwxrwt owner root name=tmp

permission drwxr-xr-x owner root name=usr

permission drwxr-xr-x owner root name=var

permission lrwxrwxrwx owner root name=vmlinuz

permission lrwxrwxrwx owner root name=vmlinuz.old

Lets split that up a bit to understand it.

awk ### is the command

‘{print “permission”,$1,”owner”,$3,”name=”$8}’ ### and the remainder of the line is the arguments

In the arguments I am asking to print on the terminal the columns ($1, $3, $8) and I am asking for the words to be printed before the columns (these are the words between double quotes : “permission”, “owner”, “name). So command first awk followed by the arguments between ‘{ and }’

Note : The columns go in logical, i.e. if we want to call column 1, then that would be $1, if we wanted to call column 5, that would be $5, etc

Summary

Good command to manipulate the specific columns of data you want to see

Advertisements

From → Linux

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: