Learn your SED command — Part 2

After the part 1 of sed command i tried to explain the 2nd with more simple way . Hope you will understand good .

SED Commands:

Till now we see various options of sed command with only one command .i.e s – substitution.

But sed have many commands.

Below are the various sed commands which we will use for various requirements.

  1. Substitution (/s):

Sed has many commands but most of the people are aware of command “ s “ . You can remember s for substitution.

As I already explained initially in an example that we can replace or substitute one word with other word in the entire file.

Example:There a file called “sample” and you need to modify the contents of a file like need to replace a word “crom” into “cron” on every line for its first occurrence.

The command that fill above requirement is

E.g. : sed ‘s/crom/cron’ sample

Suppose if we have

  • cat sample
  • crom has two files, crom.allow and crom.deny
  • After issue of sed ‘s/crom/cron’ sample
  • o/p : cron has two files, crom.allow and crom.deny.

Here it replaced only the first occurrence of crom into cron and it didn’t replace rest.

So your doubt is how to replace every occurrence of crom into cron right ?

You remember above I explained the sed option with substitution command using

                       sed -i ‘s/crom/cron/g’ sample

Where “/g “is called flag(suffix) and known as g for global.

We will discuss about flags later on.


  1. Append (a\) :

The “a\” command appends a line after the range or pattern.

See the below example, I append a newline with text “this file is appending after whenever the word cron will come” whenever the word cron existed in the line.

E.g.: sed ‘/cron/ a\ this file is appending after whenever the word cron will come ‘ sample

sed2.png

If you want to add more lines, each line should be separated by “\” .


  1. Change (c\) :

The “c\” command changes the current line with new line.

E.g.: sed ‘/two/ c\ chaning the line which contains the word two’ sample

sed2.png


  1. Deletion (d) :

Till now we saw appending ,changing and inserting of data into the file ,so now we have how to delete the data.

  1. Suppose you want to delete a specific line

E.g: sed ‘2d’ sample

sed2

2.We can also delete lines contain specific word

 Here in the example we deleted a line which contain the word “ two “

E.g: sed ‘/two/d’ sample

sed2.png

3.We can delete lines that matches a specific regular expression

 Suppose if we want to delete a line which contains one digital character, at least 1 digit, located at the end of the line .

E.g: sed ‘/[0-9/][0-9]*$/d’ filename

Note : Like this we can perform more deletion operations on file using d command , as of now I explained few here. There is lot to learn.


  1. Print (p) :

Another useful command is the print command: “p.” If sed wasn’t started with an “-n” option, the “p” command will duplicate the input. The command sed ‘p’ will duplicate every line.

Sed can act like grep by combining the print operator to function on all lines that match a regular expression:

sed -n ‘/match/ p’

Below example will give you a clear understanding of p command

sed2

Similarly we have many sed commands which will be very helpful in editing the files in non-interactive manner.

Below are the few more commands:

 

Command

 

Function

 

=

 

command prints the current line number to standard output

 

H

 

Copies the pattern buffer into the hold buffer. The pattern buffer is unchanged

 

X

 

exchanges the hold buffer and the pattern buffer

 

N

Will print out the current pattern space (unless the “-n” flag is used), empty the current pattern space, and read in the next line of input.
 

R

 

For reading the file

 

W

 

Write file name

 

Y

 

Transform like tr command

That’s it for second part . I will cover some more basics of sed command in Part3.

Don’t forgot to post your quires and feedback.

Advertisements

Express your Opinion here

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