sed command under linux

sed is a stream editor, which is a very useful tool in text processing. It can be used perfectly with regular expressions and has extraordinary functions. When processing, store the currently processed line in a temporary buffer, called "pattern space", and then use the sed command to process the contents of the buffer. After the processing is complete, send the contents of the buffer to the screen. Then the next line is processed, and so on, until the end of the file. The file contents are not changed unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.

sed options, commands, substitution tokens

command format

sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)

Options

-e<script> or --expression=<script>: Process the input text file with the script specified in the option;
-f <script file> or --file=<script file>: Process the input text file with the script file specified in the option;
-h or --help : display help;
-n or --quiet or --silent: only display the results of script processing;
-V or --version: Display version information.

parameter

Files: Specifies a list of text files to process.

sed command

a\ Insert text below the current line.
i\ Insert text above the current line.
c\ Change the selected line to new text.
d delete, delete the selected row.
D delete the first line of the template block.
s replaces the specified character
 h and copies the contents of the template block to a buffer in memory.
H Appends the contents of the template block to a buffer in memory.
g gets the contents of the memory buffer and replaces the text in the current template block.
G gets the contents of the memory buffer and appends it to the text of the current template block.
lList cannot print a list of characters.
n reads the next line of input and processes the new line with the next command instead of the first command.
N appends the next input line to the template block and inserts a new line between them, changing the current line number.
p prints the lines of the template block.
P (uppercase) prints the first line of the template block.
qQuit Sed.
b lable branches to the marked place in the script, or to the end of the script if the branch does not exist.
r file reads lines from file.
t labelThe if branch, starting from the last line, once the condition is met or the T, t command, will cause a branch to the command with the label, or to the end of the script.
T label error branch, starting from the last line, once an error or T, t command occurs, it will cause a branch to the command with the label, or to the end of the script.
w file Writes and appends the template block to the end of file.  
W file writes and appends the first line of the template block to the end of file.  
! Indicates that the following command will act on all unselected lines.  
= prints the current line number.  
# Extend the comment up to the next newline.  

sed replace tag

g means full inline replacement.  
p means print line.  
w means write the line to a file.  
x means to swap the text in the template block and the text in the buffer.  
y means to translate a character into another character (but not used in regular expressions)
 \1 substring matching token
 & matched string token

sed metacharacter set

^ matches the beginning of a line, eg: /^sed/ matches all lines starting with sed.
$ matches the end of the line, eg: /sed$/ matches all lines ending with sed.
. matches any character that is not a newline, such as: /sd/ matches s followed by any character, and finally d.
* matches 0 or more characters, eg: /*sed/ matches all lines where the template is one or more spaces followed by sed.
[] matches a specified range of characters, such as /[ ss ] ed / matches sed and Sed.  
[^] matches a character that is not in the specified range, such as: /[^A-RT-Z]ed/ matches the beginning of a letter that does not contain AR and TZ, followed by the line of ed.
\(..\) matches substrings, saves the matched characters, such as s/\(love\)able/\1rs, loveable is replaced by lovers.
& Save the search characters to replace other characters, such as s/love/**&**/, love becomes **love**.
\< matches the beginning of a word, e.g.:/\<love/ matches a line containing the word starting with love.
\> matches the end of a word, eg /love\>/ matches a line containing a word ending in love.
x\{m\} repeats the character x, m times, such as: /0\{5\}/ matches a line containing 5 0s.
x\{m,\}Repeat the character x at least m times, such as: /0\{5,\}/ matches a line with at least 5 0s.
x\{m,n\} repeats the character x, at least m times, not more than n times, such as: /0\{5,10\}/ matches 5~10 lines of 0.

sed usage example

Substitute operation: s command

Replace strings in text:

sed 's/book/books/' file

Use the -n option with the p command to print only those lines where the substitution occurs:

sed -n 's/test/TEST/p' file

Edit the file directly with the option -i , which will match the first book of each line in the file and replace it with books:

sed -i 's/book/books/g' file

full replacement mark g

Using the suffix /g flag replaces all matches on each line:

sed 's/book/books/g' file

When you need to replace from the Nth match, you can use /Ng:

echo sksksksksksk | sed 's/sk/SK/2g'
skSKSKSKSKSK

echo sksksksksksk | sed 's / sk / SK / 3g'
skskSKSKSKSK

echo sksksksksksk | sed 's / sk / SK / 4g'
skskskSKSKSK

delimiter

The character / in the above command is used as a delimiter in sed, or any delimiter can be used:

sed 's:test:TEXT:g'
sed 's|test|TEXT|g'

Delimiters need to be escaped when they appear inside styles:

sed 's/\/bin/\/usr\/local\/bin/g'

Delete operation: d command

Remove blank lines:

sed '/^$/d' file

Delete line 2 of the file:

sed '2d' file

Delete all lines from line 2 to the end of the file:

sed '2,$d' file

Delete the last line of the file:

sed '$d' file

Delete all lines starting with test in the file:

sed '/^test/'d file

Matched string token &

The regular expression \w\+ matches every word, replacing it with [&], and the & corresponds to the word that was matched before:

echo this is a test line | sed 's/\w\+/[&]/g'
[this] [is] [a] [test] [line]

All lines starting with 192.168.0.1 will be replaced with itself plus localhost:

sed 's/^192.168.0.1/&localhost/' file
192.168.0.1localhost

Substring matching token \1

Match part of the given style:

echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number

The digit 7 in the command is replaced with 7. The substring matched by the pattern is 7, \(..\) is used to match the substring, the first substring matched is marked as  \1 , and so on, the second result matched is  \2 , E.g:

echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa

love is marked as 1, all loveables will be replaced with lovers, and printed:

sed -n 's/\(love\)able/\1rs/p' file

Combine multiple expressions

sed 'expression' | sed 'expression'

Equivalent to:

sed 'expression; expression'

quote

sed expressions can be quoted using single quotes, but double quotes are required if the expression contains variable strings.

test=hello
echo hello WORLD | sed "s/$test/HELLO"
HELLO WORLD

Range of selected lines: , (comma)

All lines within the range determined by the templates test and check are printed:

sed -n '/test/,/check/p' file

Print all lines starting from line 5 to the first containing line starting with test:

but -n '5,/^test/p' file

For lines between template test and west, replace the end of each line with the string aaa bbb:

sed '/test/,/west/s/$/aaa bbb/' file

Multipoint editing: e command

The -e option allows multiple commands to be executed on the same line:

sed -e '1,5d' -e 's/test/check/' file

The first command of the sed expression above deletes lines 1 to 5, and the second command replaces test with check. The order in which the commands are executed has an effect on the result. If both commands are substitution commands, the first substitution command will affect the result of the second substitution command.

The command equivalent to -e is --expression:

sed --expression='s/test/check/' --expression='/love/d' file

Read from file: r command

The contents of the file are read in and displayed after the line matching test. If multiple lines are matched, the contents of the file will be displayed below all matching lines:

sed '/test/r file' filename

Write to file: w command  

In the example all lines containing test are written to file:

sed -n '/test/w file' example

Append (below line): a\command

Append this is a test line to the line starting with test:

sed '/^test/a\this is a test line' file

Insert this is a test line after line 2 of the test.conf file:

sed -i '2a\this is a test line' test.conf

Insert (on line): i\command

Append this is a test line to the line starting with test:

sed '/^test/i\this is a test line' file

Insert this is a test line before line 5 of the test.conf file:

sed -i '5i\this is a test line' test.conf

Next: n command

If test is matched, move to the next line of the matching line, replace the aa of this line with bb, print the line, and continue:

sed '/test/{ n; s/aa/bb/; }' file

Morph: y command

Convert all abcde in lines 1 to 10 to uppercase. Note that regular expression metacharacters cannot be used with this command:

sed '1,10y/abcde/ABCDE/' file

Exit: q command

After printing line 10, exit sed

sed '10q' file

Hold and Get: h Command and G Command

As sed processes a file, each line is kept in a temporary buffer called the pattern space, and unless the line is deleted or the output is canceled, all processed lines are printed to the screen. Then the pattern space is emptied, and a new line is stored for processing.

but -e '/test/h' -e '$ G' file

In this example, after a line matching test is found, it is stored in the pattern space, and the h command copies it and stores it in a special buffer called the hold buffer. The second statement means that when the last line is reached, the G command fetches the line that holds the buffer, puts it back into the pattern space, and appends to the end of the line that now exists in the pattern space. In this example it is appending to the last line. Simply put, any line containing test is copied and appended to the end of the file.

Keep and swap: h command and x command

Swap the pattern space and hold the contents of the buffer. That is, swap the lines containing test and check:

sed -e '/test/h' -e '/check/x' file

script scriptfile

A sed script is a list of sed commands, and the script filename is booted with the -f option when starting Sed. Sed is very picky about the commands entered in the script, there cannot be any whitespace or text at the end of the command, and if there are multiple commands on one line, separate them with semicolons. Lines starting with # are commented out and cannot span lines.

sed [options] -f scriptfile file(s)

print odd or even lines

method 1:

sed -n 'p;n' test.txt #odd lines
sed -n 'n;p' test.txt #even lines

Method 2:

sed -n '1~2p' test.txt #odd lines
sed -n '2~2p' test.txt #even lines

print the next line of the matched string

grep -A 1 SCC URFILE
sed -n '/SCC/{n;p}' URFILE
awk '/SCC/{getline; print}' URFILE

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521457&siteId=291194637