首页 > 代码库 > Note of sed

Note of sed

refers to <Linux Command Line and Shell Scripting Bible>

date:Sep 17 2016

 

format for using sed command:

           set options script file

 


 

command of ‘s‘,the s command substitutes a second text string for the first text string pattern specified between the forward slashes.

技术分享

(The sed command executes and returns the data almost instantaneously. As it processes each line of data,the results are displayed,You‘ll start seeing results before the sed editor completes processing the entire file.)

技术分享


To execute more than one command from the sed command line, just use the -e option:

技术分享

and I notice this: the second command will edit the text base on the first command‘s result.

技术分享


 

If you have lots of commands you want to process, it is often easier to just store them in a separate file.Use -f option to specify the file in the sed command:

技术分享    

(tip:It can be easy to confuse your sed editor script files with your bash shell script files. To eliminate confusion, use a .sed file extension on your sed script files.)


 The substitute command works fine in replacing text in multiple lines, but by default,it replaces only the first occurrence in each line.

技术分享

So you must use a substitution flag.

Four types of substitution flags are available:

         A number,indicating the pattern occurrence for which new text should be subsittuted.

技术分享

        g, indicating that new text should be substituted for all occurrences of existing text.

技术分享

        p,indicating that the contents of the original line should be printed.

技术分享

        w file,which means to write the results of the subsititution to a file.

技术分享


 

Note of sed