Detailed Sed command usage and regular expression metacharacters

sed command usage

sed is a stream editor, it is a very useful text processing tool that can perfectly fit the regular expression to use, extraordinary. When processing, the line currently being processed is stored in a temporary buffer, called a "pattern space" (pattern space), followed by treatment with the contents of the buffer sed command processing is completed, the contents of the buffer sent to the screen. Then the next line, which is repeated until the end of the file. File contents not changed, unless you use redirection to store the output. sed primarily used to automatically edit one or more files, the file operation is repeated to simplify the preparation of the conversion procedures.

1 Introduction

sed non-interactive editor. It does not modify the file, unless you use shell redirection to save the results. By default, all output rows are printed to the screen.

sed file editor processed line (or input), and transmits the result to the screen. Specific process is as follows: First, the sed line currently being processed is stored in a temporary buffer area (also referred to as spatial mode), and the processing in the temporary buffer rows, the rows after the completion of the transmission to the screen. sed finished processing each row will be deleted from the temporary buffer, then reading the next row, for
processing and display. After processing the last line of the input file, sed is ended run. Each line sed the presence of a temporary buffer, this copy editor, so it will not modify the original file.

2. addressing

Addressing is used to determine which rows to edit. The address may be a digital form, a regular expression, or a combination of both. If the address is not specified, sed will process all the input file lines.

Address is a number that indicates the line number; the "$" symbol, it indicates that the last line. For example: sed -n '$ p' / etc / issue 

3. Command and Option

sed sed command tells how to handle each input line designated by the address, if the address is not specified process all input lines.

3.1 sed command

a\

Add one or more lines after the current line. Multiple lines except the last, the end of each line of the file required "\" line-continuation -a, just shows that if we really want to change the text, it is necessary sed -i.bak '' This will come out with a bak format this file is a source file

[Root @ entos74 app #cat -N passwd.txt | sed '10, 20a \ 11 adadadadadada

c\

Alternatively in the current line of text after the new text for this symbol. Multiple lines except the last, at the end of each line required "\" line-continuation

cat -n passwd.txt | sed '10c\XXXXXXXXXXXXXXXXXX'

  i \
    Insert text before the current line. Multiple lines except the last, the end of each line required "\" line-continuation
    CAT -n passwd.txt | sed '10i \ xxxxxxxxxxxxxxxxxx'
  d
  delete rows: ss -ntl | sed '1d' delete the first line
  h
  the mode space to copy the contents of staging buffer   
  H
  to the pattern space to the contents of the temporary buffer is added
  g
  copy the contents of the temporary buffer to the pattern space, overwriting the old contents
  G
  contents of the temporary buffer mode added to the space, be appended to the original content of
  p
 after the current print mode spatial content, is added to the default output
  n
  reading the next input line, and not the next command from the first command processing starts thereto
  q
  end or exit Sed
  R & lt
  read from the file input line
  !
  all lines other than the selected row application commands
  s
  replaced by a string of another
  g
  inline globally Alternatively: CAT -n passwd.txt | Sed  's @ / bin / the bash $/ sbin / nologin @ G '
  w
  The write line selected files: Sed '/ ^ lixiaozi / w /app/lixiaozi.txt' passwd.txt 
  X
  exchange temporary buffer contents and the model space
  y
  replaces the character with another character (not on a regular expression uses y command)  

3.2 sed options

  Option
  Function
  -e
  make multiple editing, namely the use of multiple input line application command sed
  -n
  cancel the default output
  -f
  specify sed script file name

4. Exit state

sed grep not to the same, regardless of whether or not find the specified pattern, its exit status is 0. Only when there is a syntax error command, the exit status of sed it was not 0.

The regular expression metacharacters

  As with grep, sed also supports special metacharacters for pattern find, replace. Except that, sed use regular expression pattern is enclosed between the "/" in slash lines.

If the regular expression should delimiter "/" to another character, such as O, just add a backslash character in front of this, to keep up with the regular expression character, this character to talk. For example: sed -n '\ o ^ Myop ' datafile
    Metacharacter
  function
  exemplary
  ^
  of the line locator
  / ^ my / matches all lines starting with my
  $
  end of line locator
  / my $ / ending my match all rows
  .
  Match single newline character except
  /m..y/ comprises matching letters m, followed by any two characters talk row of letter y
  *
  matches zero or more of the preceding character
  / my * / matching contain letters m, followed by zero one or more rows of letters y
  []
  matches specify any character in the character set of
  row / [Mm] y / My match or comprise a my
  [^]
  matches not specify any character in the character set
  / [^ Mm] y / matching comprises y, but that the character is not m or m before the line y
  \ (.. \)
  to save the character that has been matched
  1,20s / \ (you \) self / \ pattern between 1r / marking character element, and save it as a label, you may then be used \ 1 to refer to it. Can define up to nine labels, numbered from the left, the far left is the first. In this example, a first line 20 to the second processing, you are saved as a label, youself if found, replace your.
  &
  Saved string to find the reference string in alternative
  s / my / ** & ** / & Representative symbol lookup string. will be replaced my my ** **
  \ <
  first word locator
  / \ <my / my begin comprising matching word line
  \>
  suffix locator
  / my \> / matching word contains my ending row
  x \ {m \}
  m consecutive X
  / 9 \ {5 \} / matching comprises 5 consecutive 9 rows
  x \ {m, \}
  at least m X
  / 9 \ {five, \} / matching comprises at least 5 consecutive 9 is a line
  x \ {m, n \}
  at least m but not more than n X
  / 9 \ {5,7 \} / match 5-7 comprises a continuous row 9

6. Examples  

6.1 p command

P for content display mode command space. By default, sed the input line printed on the screen, the default option -n to cancel the print operation. When the -n option and command p occur simultaneously, sed can print the selected content.  

Sed '/ My / P' datafile
# default, sed all input line printed on the standard output. If a line matching pattern my, p command will print the additional line again.
[root @ entos74 ~] #sed -n '3p' filelist.txt
print only the third line
displays only the contents of the file specified line range, for example:
# view files only, line 20 to line 30, and line number belt 
cat -n / etc / passwd | sed -n  '20, 30p '

6.2 d command

D command to delete the entry line. sed first line of input from a file to copy the pattern space, and then the sed command line execution, and finally the pattern space to the contents displayed on the screen. If you issue a command d, the current pattern space in the entry line will be deleted, it is not displayed.

sed '$ d' datafile
# delete the last line, and the rest are displayed
CAT -n passwd.txt | sed '/ mail /, 25d' 
# delete contains "mail" line to the contents of the first 25 lines of
address is a comma-separated , then the address is the need to address a range of rows between the two (including two lines included). With the numeric range, a regular expression, or a combination of both FIG. For example:
Sed '2,5d' datafile
# delete the second to fifth row
Sed '/ My /, / by You / D' datafile
# Delete comprising "My" row that contains the line between "You" line

6.3 s command

Sed 'S / ^ My / by You / g' datafile
# g command indicates the end of the row replacement globally, that is to say if a line appears plurality My, My are all replaced You.  

6.4 e Options

-e is editing commands for editing the case sed to perform multiple tasks. Before the start of the next line editing, all the editing action will be applied to the pattern buffer line.

sed -e '1,10d' -e 's/My/Your/g' datafile

# -E option for multiple editing. The first re-edited to delete lines 1-3. All My second re-edited to appear replace Your. Because these two line by line editing (ie these two commands are executed on the current line of the pattern space), so editing commands in the order will affect the results.

6.5 w command

sed -n '/hrwang/w me.txt' datafile 

6.6 a \ command

A \ command is a command to append, to add new text is added to the current line in the file back (i.e., read into the pattern buffer lines). The additional line of text beneath located sed command on a separate line. If you want to append the contents of more than one line, then each line must end with a backslash, except the last one. The last line will end with quotes and file name.

Sed '/ ^ hrwang / A \
> hrwang are Husband and mjfan \
> and wife' datafile
# If a match is found in the line beginning hrwang datafile file, added hrwang and mjfan are husband and wife in the row below  

6.7 i \ command

i \ command is to insert new text in front of the current line.  

6.8 c \ command

sed Use this command to modify the existing text into a new text.  

6.9 n command

sed using this command to obtain the next line of the input file, and it is read into the buffer mode, no sed command will be applied to the upper and lower line matching lines immediately.

sed '/hrwang/{n;s/My/Your/;}' datafile

to sum up

The above is the use of small series to introduce Detailed Sed commands and regular expression metacharacters, we want to help, if you have any questions please give me a message, Xiao Bian will promptly reply to everyone. In this I am also very grateful for the support of the home-site scripting!

 

Published 133 original articles · won praise 129 · views 330 000 +

Guess you like

Origin blog.csdn.net/wofreeo/article/details/103812462