Linux sed (stream editor) – part 2

Continuing from  part 1 of Linux Sed.

If we want to run more than one sed’s command on the text, we can use semicolons ‘;’ or the -e . the structure is the following:
sed -e '' -e ''
Alternatively and more elegant is the semicolons  separator:
sed '<command1>;<command2>;'  <fileName>

Now let continue with the sed commands’ list:

  • =
    The equal command print the line number for every line its get, and then continue. In the simple form, we get line number for every line and below it the content line.
    Example:

    
    oren@oren-computer:~/c-programming  
    $ sed '=' arguments.c
    1
    #include <stdio.h>
    2
    #include <stdarg.h>  
    3
    double sum(float num,...){
    4
    	float i;
    5
    	double result,arg;
    6
    	va_list arglist;
    7
    	va_start(arglist, num);
    8
    	result=0;
    9
    	for (i=0;i<num;i++){
    10
    		arg=va_arg(arglist,double);
    11
    		result+=arg;
    12
    		prfloatf("results=%f arg=%f\n",result,arg);
    13
    	}
    14
    	va_end(arglist);
    15
    	return result;
    16
    }
    17
    
    18
    float main(){
    19
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    20
    }
    
    
  • Sed also have filter for the commands, the filter appear before the command separated by space. If there is a match between the  filter and the text line  the command will run on this line. We can have filter base on the line number or regular expression.
    The structure is the following:
    sed ‘<filter> <command>’ <fileName>

      • We can have a filter according to regular expression matching sting inside the line.
        sed '/<regex>/ <command>;' <fileName>
        and it will execute the command only one line that match the regex.
        In the below example we print all file content in the first command, and as a second command we run sed (print) command  on all the line that include the word ‘double’, we do this by using the regex filter:

        
        oren@oren-computer:~/c-programming  
        $ sed '' arguments.c
        #include <stdio.h>
        #include <stdarg.h>
        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 -n '/double/ p' arguments.c
        double sum(float num,...){
        	double result,arg;
        		arg=va_arg(arglist,double);
        
        
        

        In the example above the filter was /double/, and the command was p. so we only execute the command of print on-line that match the regex. Regex filter are surrounded with forward-slash

      • We also have line number filter, it get two numbers separate  by comma.The first number is the first line on which we want to execute the command and the second line is the last line we want to execute the command, of course it will execute the command on all the lines in between. The command structure is the following:
        sed '<firstLine>,<lastLine> <command>;' <fileName>
        In the below example, we run sed with line number filter, for the lines between 2,5, on the file from the example above:

        
        oren@oren-computer:~/c-programming  
        $ sed -n '2,5 p' arguments.c
        #include 
        double sum(float num,...){
        	float i;
        	double result,arg;
        
        
        

        And indeed it printed all the lines from 2 to 5 (include 5).

      • We can have range filter based on two regex, and it will execute the command on all the line between the first occurrence of the  first regex and the last occurrence of the last regex. It select the widest possible range. The structure is the following:
        sed '/<firstRegex>/,/<secondRegex>/ <command>' <fileName>
        In the below example we run the p command on all the lines between the first occurrence of the word double and the last occurrence of the word result.We run it on the same file from the example below:

        
        oren@oren-computer:~/c-programming  
        $ sed -n '/double/,/result/ p' arguments.c
        double sum(float num,...){
        	float i;
        	double result,arg;
        		arg=va_arg(arglist,double);
        		result+=arg;
        
        
      • We can have filter that start in a specific line, and then count from this line a number of lines. For this we use the sign plus ‘+’.
        The structure is the following:
        sed '<startingLineNumber>,+<numberOfLine> <command>;' <fileName>
        In the below example, we will run print command from line 2 to two lines below it:

        
        $ sed -n '2,+2 p' arguments.c
        #include <stdarg.h>
        double sum(float num,...){
        	float i;
        
        

     

     

  • we can also mix and match filters. some example:
    • In the example below we have filter from the first occurrence of the word ‘double’, to two lines below:
      
      oren@oren-computer:~/c-programming  
      $ sed -n '/stdarg/,+2 p' arguments.c
      #include 
      double sum(float num,...){
      	float i;
      
      
    • In the second example, we will filter from the third line to the last occurrence of ‘va_end’
      
      oren@oren-computer:~/c-programming  
      $ sed -n '3,/va_end/ p' arguments.c
      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);
      
      
      
  • if we want to run more than one command for a filter we can use curly braces.
    The structure is
    sed -n '<filter> {<command1>; <command2>; <command3>;...}' <fileName>
    In the below example, we will run 2 substitute commands on the lines between 3 to 6 :

    
    oren@oren-computer:~/c-programming  
    $ sed -n '3,6 {s/sum/min/; s/arg/param/;p}'  arguments.c
    double min(float num,...){
    	float i;
    	double result,param;
    	va_list paramlist;
    
    
    

click for sed part 3

Advertisements

2 thoughts on “Linux sed (stream editor) – part 2

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