Linux sed (stream editor) – part 1

Sed is a stream editor. We can use it to preform text transformation on input stream (regular file or input stream).
The basic structure of sed is:
sed <option> '<command>' <fileName>
In this case it will change the file content according to the <command> and print it to the screen, the original file will be unchanged.

sed options:

  • -i
    If we want to change the original file we can use the ‘-i’ option. We cant attach to it dot prefix (‘-i’.<backupPrefix>) and it will create a back up file with the same name as the original concat with the prefix.
    The structure will be the following:
    sed -i.<backupPrefix> '<command>' <fileName>
    In this case file <fileName> will be changed, but a backup file with the name <fileName>.<backupPrefix> and the original content of file <fileName> will be created.
  • -n
    This option surpass default print . By default sed read the input stream line by line, preform text transformation on it and then print it. Using the -n prevent the default print.
    It will print only lines, that the command tell it to print.
    This option is useful to combine with the  command, to filter text, and display only matching text (whitelist filter).For our example we have the below index.html file

    
    oren@oren-computer:~/workspace/sass  (master) 
    $ cat index.html
    <!DOCTYPE html>
    <html>
      <head>
    <link rel="stylesheet"  href="style/main.css">
    
      </head>
      <body >
       
       
      
        
      </body>
    
    </html>
    

    We will use the -n option to prevent the default print, and use p command to print the lines between line 2 and line 10 (including):

    
    oren@oren-computer:~/workspace/sass  (master) 
    $ sed -n '2,10 p' index.html 
    <html>
      <head>
    <link rel="stylesheet"  href="style/main.css">
    
      </head>
      <body >
       
      
    
    

 

 

 

sed commands:

  • p stand for print.
    it tell sed print specific line, it useful when we are using the -n, otherwise those lines will be printed twice.
    sed -n '<confitionToMatch> p;' filename
    In the example below, we do

    1. run sed to print all lines
    2. run sed to print all lines between 1 to 5
    3. run sed to print all lines include “body”
    
    oren@oren-computer:~/workspace/sass  (master) 
    $ sed -n 'p;' index.html 
    <!DOCTYPE html>
    <html>
      <head>
    <link rel="stylesheet"  href="style/main.css">
    
      </head>
      <body>
    
    
    
    
      </body>
    
    </html>
    oren@oren-computer:~/workspace/sass  (master) 
    $ sed -n '1,5 p;' index.html 
    <!DOCTYPE html>
    <html>
      <head>
    <link rel="stylesheet"  href="style/main.css">
    
    oren@oren-computer:~/workspace/sass  (master) 
    $ sed -n '/body/ p;' index.html 
      <body>
      </body>
    
    
    
  • d stand for delete
    delete the matching line and move on to next line.
    sed '<conditionToMatch> d;' filename
    In the example below, we will print file content with cat, then will use sed to print file content but delete all lines contain “body” string.

    
    oren@oren-computer:~/workspace/sass  (master) 
    $ cat index.html 
    <!DOCTYPE html>
    <html>
      <head>
    <link rel="stylesheet"  href="style/main.css">
    
      </head>
      <body>
       
       
      
        
      </body>
    
    </html>
    oren@oren-computer:~/workspace/sass  (master) 
    $ sed '/body/ d;' index.html 
    <!DOCTYPE html>
    <html>
      <head>
    <link rel="stylesheet"  href="style/main.css">
    
      </head>
       
       
      
        
    
    </html>
    
    

    It can useful to remove empty line, we can do this by running
    sed '/^$/ d' filename

    Running on the file from previous example, will only show non empty line in this file:

    
    oren@oren-computer:~/workspace/sass  (master) 
    $ sed '/^$/ d;' index.html 
    <!DOCTYPE html>
    <html>
      <head>
    <link rel="stylesheet"  href="style/main.css">
      </head>
      <body>
      </body>
    </html>
    
    

    Just like we can use p to print specific lines according to their lines number, we can use d to delete specific lines according to their lines number.

    In the example below, we delete line 1 to 5:

    
    oren@oren-computer:~/workspace/sass  (master) 
    $ sed '1,5 d;' index.html 
      </head>
      <body>
    
    
    
    
      </body>
    
    </html>
    
    
  • s stand for substitute. we give it two regular expression, one for the pattern to find and one for the pattern to replace it.
    sed 's/<patternToMatch>/<newPattern>/<flags>' <filename>
    if the pattern occur more than once in a line and we want to replace all occurrences of the selected pattern then we need to use the g modifier that stand for global.In the example below we will show a c program source file and then we will run sed with substitution command to replace all ‘double’ with ‘float’

    
    oren@oren-computer:~/c-programming  
    $ cat arguments.c
    #include 
    #include 
    double sum(float num,...){
    	float i;
    	double result,arg;
    	va_list arglist;
    	va_start(arglist, num);
    	result=0;
    	for (i=0;i<num;i++){
    		arg=va_arg(arglist,double);
    		result+=arg;
    		prfloatf("results=%f arg=%f\n",result,arg);
    	}
    	va_end(arglist);
    	return result;
    }
    
    float main(){
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    oren@oren-computer:~/c-programming  
    $ sed 's/double/float/g;' arguments.c
    #include 
    #include 
    float sum(float num,...){
    	float i;
    	float result,arg;
    	va_list arglist;
    	va_start(arglist, num);
    	result=0;
    	for (i=0;i<num;i++){
    		arg=va_arg(arglist,float);
    		result+=arg;
    		prfloatf("results=%f arg=%f\n",result,arg);
    	}
    	va_end(arglist);
    	return result;
    }
    
    float main(){
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    
    

for part 2 click

Advertisements

One thought on “Linux sed (stream editor) – part 1

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