Skip to content
Tags

Enough sed

January 7, 2011

sed is similar to awk but with one main difference. The sed command processes text from rows and prints them out. For example lets say that in the below example we want to see all the rows that contain the size 4.0K.

prompt> ls -lh

total 96K

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

drwxr-xr-x 3 root root 4.0K 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 4.1K 2011-01-04 20:02 dev

drwxr-xr-x 144 root root 12K 2011-01-04 20:04 etc

drwxr-xr-x 3 root root 4.0K 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 16K 2010-11-24 19:46 lib

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

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

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

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

dr-xr-xr-x 149 root root 0 2011-01-04 20:02 proc

drwx—— 11 root root 4.0K 2010-12-03 17:31 root

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

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

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

drwxr-xr-x 12 root root 0 2011-01-04 20:02 sys

drwxrwxrwt 19 root root 4.0K 2011-01-04 20:14 tmp

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

drwxr-xr-x 15 root root 4.0K 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

Then what we would do is call sed to print the specific rows that contain the characters 4.0K like in the example below: –

prompt> ls -lh | sed -n ‘/4.0K/p’ ### -n is used to only print the lines containing the pattern

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

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

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

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

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

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

drwx—— 11 root root 4.0K 2010-12-03 17:31 root

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

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

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

drwxrwxrwt 19 root root 4.0K 2011-01-04 20:14 tmp

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

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

Similarly if we want to see all rows that do not contain 4.0K then we would use the same command but different argument as below: –

prompt> ls -lh | sed ‘/4.0K/d’ #### d for do not display the rows that contain the characters between and

total 96K

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

drwxr-xr-x 18 root root 4.1K 2011-01-04 20:02 dev

drwxr-xr-x 144 root root 12K 2011-01-04 20:04 etc

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 16K 2010-11-24 19:46 lib

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

dr-xr-xr-x 150 root root 0 2011-01-04 20:02 proc

drwxr-xr-x 12 root root 0 2011-01-04 20:02 sys

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

As you can see in the above example the rows that contained the data of 4.0K has been omitted from the printed data.

Furthermore sed can be used to replace the printed output of a command/files contents from one string of characters to another string of characters, e.g. lets say on the above example we want to substitute the word root with ADMIN, then this is how we would do it: –

prompt> ls -lh | sed -e ‘/4.0K/d’ -e ‘s/root/ADMIN/g’

total 96K

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

drwxr-xr-x 18 ADMIN ADMIN 4.1K 2011-01-04 20:02 dev

drwxr-xr-x 144 ADMIN ADMIN 12K 2011-01-04 20:04 etc

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

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

drwxr-xr-x 20 ADMIN ADMIN 16K 2010-11-24 19:46 lib

drwx—— 2 ADMIN ADMIN 16K 2010-02-10 17:59 lost+found

dr-xr-xr-x 150 ADMIN ADMIN 0 2011-01-04 20:02 proc

drwxr-xr-x 12 ADMIN ADMIN 0 2011-01-04 20:02 sys

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

lrwxrwxrwx 1 ADMIN ADMIN 30 2010-07-31 15:34 vmlinuz.old -> boot/vmlinuz-2.6

Note: – when using more than one string as arguments we need to add -e to tell the command to execute the argument. The s in the above example is telling sed to substitute the string root to ADMIN and the g is used to enforce the substitute

Summary

A good command to manipulate what rows you want to see based on the characters that you choose to display or not within the rows

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: