Sed Command Parsing

Sed Tool Overview

Stream EDitor, a Stream compiler

  • Non-interactive, filtering and modifying text based on pattern matching
  • Process it line by line and print the results to the screen
  • Can realize the output, delete, replace, copy, cut, import, export and other operations of text

Command Syntax description

Main usage

  • Format: 1 front command | sed [option] ‘edit command’
  • Format 2: sed [option] ‘edit instructions’ file
[root~]# sed -n ’^id‘ /etc/inittab 
Copy the code

Common command options

  • -n: Mask default output (all text)
  • -i: Directly modifies the file content
  • -r: starts the extended regular expression

conditions

  • Line numbers can be numeric to indicate single lines
  • 3 and 5 represent consecutive lines
  • Omitting the condition defaults to processing all text line by line
  • To match a re, use //

Basic processing actions

  • Common action command

Common Processing operations

The output text

Sed -n 'p' a.t # Sed -n '/^bin/p' a.t # sed -n '$=' a.t # sed -n '/^bin/p' a.t # sed -n '$=' a The number of lines in the output fileCopy the code

Delete the text

Sed '/ XML /d 'a.txt # sed '/ XML /! Sed '/ XML /! D 'a.txt # delete rows that do not contain XML,! In this example, only output is done and no changes are made to the original file. (If changes are needed, add the -i option.)Copy the code

Replace text

Sed 's/ XML/ XML/' a t t # replace the first XML in each line with XML sed 's/ XML/ XML/3' A t t # Replace the third XML in each line with XML sed 's/ XML/ XML/g' a t t # Replace all XML with XMLCopy the code

Sed text processing action