Linux Sed 命令指南 (Sed command cheatsheet)

By Long Luo

$ sed –help

Usage: sed [OPTION]… {script-only-if-no-other-script} [input-file]…

-n, –quiet, –silent suppress automatic printing of pattern space –debug annotate program execution -e script, –expression=script add the script to the commands to be executed -f script-file, –file=script-file add the contents of script-file to the commands to be executed –follow-symlinks follow symlinks when processing in place -i[SUFFIX], –in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied) -l N, –line-length=N specify the desired line-wrap length for the `l’ command –posix disable all GNU extensions. -E, -r, –regexp-extended use extended regular expressions in the script (for portability use POSIX -E). -s, –separate consider files as separate rather than as a single, continuous long stream. –sandbox operate in sandbox mode (disable e/r/w commands). -u, –unbuffered load minimal amounts of data from the input files and flush the output buffers more often -z, –null-data separate lines by NUL characters –help display this help and exit –version output version information and exit

If no -e, –expression, -f, or –file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read.

GNU sed home page: https://www.gnu.org/software/sed/. General help using GNU software: https://www.gnu.org/gethelp/. E-mail bug reports to: .

Sed Commands

Options Description

Exit sed without processing any more commands or input. Delete the pattern space; immediately start next cycle. Print out the pattern space (to the standard output). Appending text after a line. insert text before a line. = Print out the current input line number (with a trailing newline). Replaces the line(s) with text. w Write the pattern space to filename. Empty the content of the pattern space. Delete text in the pattern space up to the first newline , restart cycle if pattern space contains newlines. Execute the command that is found in the pattern space. F Print the file name of the current input file. Reads file filename. n N Read/append the next line of input into the pattern space. W Write the first line of the current pattern space to filename Exchange the contents of the hold and pattern spaces. Transliterate any characters in the pattern space.

Command Options

Options Description

-i[SUFFIX] Edit files in place (make backup if SUFFIX supplied). -n Suppress automatic printing of pattern space. -f add the contents of script-file to the commands to be executed -e Execute multiple sed commands -r or -E Use extended regular expressions (supports ‘+’, ‘(’, ‘)’)■ -s Consider files as separate rather than as a single, continuous stream. -c Copy the input to the standard output. -I pecify the desired line-wrap length for the I command. Command Flags Flags Description g Global substitution - Replace all occurrences in each line. Execute shell command after substitution. w Save only the substituted lines to a specified file. Perform case-insensitive pattern matching. p Print only the substituted lines. 1,2,4,… Substitute the nth occurrence- Replace specific occurrence of a pattern.

BASIC EXAMPLES

$ seq |sed $ seq | sed -n $ sed ‘/warning/i # This is a warning’ logfile $ cod -o ‘<c/corvor/now-corvor/;c/port/8080/;}’ config.-conf

REPLACING TEXT $ sed s/old/new/g file.txt $ sed ‘4 s/old/new/’ file.txt $ sed s/old/new/3 file.txt $ sed ‘s/A//’ file.txt $ sed -n 7important/w important_lines.txt’ logfile $ sed 7hello/s/world/universe/’ file.txt

APPEND AND PREPEND TEXT

$ sed -n 7hello/p’ file.txt $ sed -n 7hello/lp’ file.txt $ sed -n 7hellQ/lp’ file,txt $ sed ‘2a Text after line 2’ file.txt $ sed ‘\(a THE END!' file.txt\) sed ’s/old/new/’ config.conf

DELETING LINES

$ sed 1,4d’ file.txt $ sed ‘6~4d’ file.txt $ sed ‘\(d' file.txt\) sed 7 AErrors/d’ file.txt $ sed 7 A\(/d' file.txt\) sed 7 A#/d’ file.txt $ sed ’3d’file t¥t

OPTIONS EXAMPLES

$ sed -n 7error/p’ syslog.log $ sed -e ‘s/server-01/server-02/’ -e ‘s/port=8080/port=9090/’ app.conf $ sed -i.bak ‘s/v3.3.2/3.4.1/’ app.conf $ sed .r ‘s/(error|warning)/info/’ syslog Ing $ bed -b ‘b/liubuidiiie/bei vei/1 /eiu/Luunf $ sed -n -e ’1,5C' -e’ I his is a new line.’ source.txt destination.txt $ seo -T repiace_pauerns.sea conng.ixi sysxplore.com $ sed -I long_text.txt

参考文献

  1. SED
  2. DevOps