Sed is short for Stream Editor, a stream editor that processes one line of content at a time.

The basic usage is as follows:

# sed --help
sed [OPTION]... {script-only-if-no-other-script} [input-file]...

#Sed [parameter]'Range operation'file
Copy the code

Ex. :

Sed -n '1,4 p' demo. TXT # print lines 1 to 4 of the demo. TXT fileCopy the code

Common Option Parameters

  • -nUse silent mode. With the -n argument, there is only passingsedThe line (or command) for special processing is listed.
  • -iEdit the original file directly
  • -i.bakEdit the original file directly, while generating a.bak backup file. This approach is recommended. Prevent misoperation
  • -f -f filenameCan execute in filenamesedThe command
  • -eMultipoint editor

Range of choice

Sed can be very flexible to find the content of the corresponding range. Common range options are as follows:

  • 2Select the second line
  • $Select the last line
  • 2, 5Choose 2 ~ 5 lines
  • 1 ~ 2An odd number of rows
  • 2 ~ 2Even lines
  • 1, $Line 1 to the last line of the file

Sed also supports regular matching. Such as:

  • /name/ The line where the name character appears

  • /name/,+3 3 lines after the name character

  • /^ XXX/select the line that begins with XXX

  • / XXX /,/yyy/ where XXX rows appear and the data between yyy rows appear

  • Common regular matching expressions are as follows:

    • ^ Match line start. For example: /^sed/ lines beginning with sed

    • $Matching line ends. For example, /sed$/ lines ending with sed

    • . Single character

    • * 0 or more matches

    • + 1 or more matches

    • ? Zero or one matches

    • X \{m,n\} repeated consecutive characters x,m-n times

    • ‘x{m} # repeats the character x, m times, e.g. /0{5}/ matches a line containing five zeros (consecutive).

Common operation

  • P Prints the matched content

  • D Deletes the matched content. If you need to edit the original file, use the -i or -i.bak parameters

  • What w matches is written to another file. Ex. :

    Sed -n '/ XXX/w output.txt' demo.txtCopy the code

The commonly used skill

Displays the contents of the first line

sed -n '1'p filename
Copy the code

Displays the contents of the third to last line

sed -n '3,$'p filename
Copy the code

Displays rows that match the name key

sed -n '/name/'p
Copy the code

Print a<command>Line as well as appear</command>Data between rows

sed -n '/<command>/,/<\/command>/ p'  demo.txt
Copy the code

Prints lines of at least 5 characters

 sed -n  '/^.\{5\}/ p' demo.txt
Copy the code

Delete all blank lines

sed -e '/^$/ d' demo.txt
Copy the code

Delete the space at the beginning of the line

sed -e 's/^[ \t]*//g' demo.txt
Copy the code

Remove the space at the end of the line

sed -e 's/[ \t]*$//g' demo.txt
Copy the code

Enclose each line of the file in double quotation marks

sed  -e 's/.*/"&"/g' demo.txt
Copy the code

Replace multiple lines of file content with a single line, each line filtered out with closing Spaces and enclosed in double quotes

Such as:

abc

123

Convert to the following format:

“abc”,”123″

sed -e 's/[ \t]*$//g' -e  's/^[ \t]*//g' -e '/^$/ d' -e 's/.*/"&"/g' demo.txt |tr "\n" "," |sed -e 's/,$//g'

#There is a relatively simple command that does the same thing, but the result may be different if there are Spaces or newlines in the middle of the string
echo '"'`awk '{print $1}' demo.txt |xargs |sed 's/ /","/g'`'"'
Copy the code

Linux text three Swordsmen – sed