Summary of the use and attention to sed command in linux

  • 2020-05-17 07:35:17
  • OfStack

preface

sed is a stream editor. It is a very useful tool in text processing. It can be used with regular expressions perfectly. During processing, the currently processed rows are stored in a temporary buffer called "pattern space" (pattern space), followed by the sed command to process the contents of the buffer, and when the processing is complete, the contents of the buffer are sent to the screen. Then process the next line and repeat until the end of the file. The contents of the file do not change, unless you use redirection to store the output. sed is mainly used to automatically edit one or more files, simplify the repeated operation of files, and write conversion programs.

The use of the sed

$sed [-nefr] [action]

Parameters:

-n: use quiet mode. In 1 general sed usage, all data from STDIN are listed on the screen. But if you add the -n parameter, only the row (or operation) that has been specially processed by sed will be listed. -e: do sed's action editing directly on the command line. (the default) -f: directly write sed's actions in a file, -f filename You can perform the sed action within filename. -r: sed's actions support the syntax of extended regular expressions (the default is the basic regular expression syntax). -i: directly modify the contents of the read file, not the screen output.

By default, sed does not apply directly to the file being read

Action instructions: [n1[,n2]] function

n1,n2: does not necessarily exist, 1 generally represents the number of lines selected for action. For example, if my action needs to be between 10 and 20 lines, then "10, 20[action behavior]".

function has the following parameters:

a: add to the next line, which can be followed by a string c: substitution, which can be followed by a string d: delete, not followed by jehe parameter i: insert into the previous line, followed by a string p: print, will normally run with the parameter sed-n 1 s: find and replace specific strings, usually with regular expressions.

For example, 1, 20 s/old/new/g

demo


// Suppose you have text file 

$ sed '2 . 5d' text # Will be the first 2 ~ 5 Line to delete 
$ sed '2a hahaha' text # In the first 2 Add" hahaha With the words" 
$ sed '2a hahaha\
> xixixi' text # In the first 2 Add two lines after the line 
$ sed '2 . 5c No 2-5 number' text # Will be the first 2 ~ 5 Replace the contents of the line with" No 2-5 number "Is a full line substitution 
$ sed -n '5 . 7p' text # will 5 ~ 7 Print out the line, 1 Need to add  -n
$ sed 's/hahaha/ssss/g' text # will hahaha Replace the field with ssss , which is the search and replacement of part of the data 

sed can also be used with regular expressions

Such as:


$ sed 's/^h/ssss/g' text # will h The first field is replaced with ssss

Pay attention to

sed must be followed by two single quotation marks. When using the p(print) parameter, sed1 must use -n, otherwise all the lines will be output, which makes no sense. sed does not, by default, act directly on a read file. The -i parameter should be used with caution, as it directly modifies the contents of the file being read.

conclusion


Related articles: