siwen

siwen

😉

sed command

sed Stream Editor#

Description:

sed is a stream editor, which is a very important tool in text processing. It can be perfectly used with regular expressions and has extraordinary functions. During processing, the current line being processed is stored in a temporary buffer called the "pattern space". Then, the sed command processes the content in the buffer, and after processing is complete, the content of the buffer is sent to the screen. Then, the next line is processed, and this process is repeated until the end of the file. The file content is 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.

Syntax:

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

sed Execution Process#

image

sed Command Options#

OptionFunction
-nDisable default sed output, only output the processed result, usually used with the p action
-rSupport the use of extended regular expressions
-iEdit files directly, without using -i, only the content of the buffer is modified
-eMultiple edits, same as the effect of the pipe symbol
-fRun editing commands in the sed script

sed Editing Commands#

ActionDescription
a\Append, insert text below the current line (both \ and space can be used)
i\Insert, insert text above the current line
c\Change, change the selected line to new text
dDelete, delete the selected line
pPrint, print the matched content, usually used with the -n option
sSubstitute, match and replace content, supports regular expressions
nRead the next input line and then execute the next command
wSave the line that matches the pattern to the specified file
rRead the text from the specified file into the pattern space after the matched line
=Print line numbers for lines in the pattern space
!Negate the matched line in the pattern space

sed Substitution Flags#

SymbolDescription
gReplace all occurrences in the line, if not written, it will replace the first occurrence of each line by default, ng means replacing from the n-th occurrence
\nSubstring match flag; \n represents the content in the n-th group
&Matched string flag

Explanation of the s Substitution Command

"Delimiter"

  • In the substitution command, / is commonly used as the delimiter, but you can also use custom delimiters such as :, #, @, etc.
  • When the delimiter appears internally, it needs to be escaped, such as sed 's:[0-9]:\::' test.txt uses : as the delimiter to replace all numbers with :

"Combining Multiple Expressions"

  • The sed substitution command can be used in combination with other commands. For example, replace all lowercase letters in the first line with # and print it:
    shell sed '1s/[a-z]/#/gp' test.txt
  • Other commands can also be used in combination with multiple expressions. There are three ways to write them (execute multiple commands on the same line, affected by the order):
sed 'expression1' | sed 'expression2'
sed 'expression1; expression2'
sed -e 'expression1' -e 'expression2'`

"Substring Match (Backreference)"

  • Match a part of the given pattern, used in conjunction with () grouping, \n represents the result of matching the n-th group

"Matched String"

  • Use & to represent each matched content, you can also use substring matching to achieve it

sed Matching Ranges#

RangeExplanation
Entire fileProcess the entire file
Specified lineA specific line in the file; '1p' means print the first line
Specified patternEach line matched by /pattern/; '/^H/' means lines starting with H
Specified range intervalEach line in the range; '1,3' means the first line to the third line; '1,+2' means the first line and the next two lines; '$' means the last line
Specified step sizeMatch lines based on the step size; '1~2' means 1,3,5,7,... odd lines; '2~2' means 2,4,6,8... even lines

Line ranges and pattern ranges can be combined into range intervals

sed Command Examples#

# Print the 5th line
[root@localhost ~]# sed -n '5p' test.txt

# Add the text "day day up" below each line (without modifying the original file)
[root@localhost ~]# sed 'a day day up' test.txt

# Add two lines "abcde" and "ABCDE" above the first line
[root@localhost ~]# sed -i '1i abcde\nABCDE' test.txt

# Change the last line to "66666"
[root@localhost ~]# sed -i '$c 66666' test.txt 

# Change all characters 'A' to 'a'
[root@localhost ~]# sed -i 's/A/a/g' test.txt 

# Delete the first 2 lines
[root@localhost ~]# sed -i '1,2d' test.txt

# Write lines starting with 'H' to the file file.txt 
[root@localhost ~]# sed -n '/^H/w file.txt' test.txt

# Write the contents of the file file.txt below the 5th line
[root@localhost ~]# sed -i '5r file.txt' test.txt

# Delete the line starting with 'l' and the next line
[root@localhost ~]# sed -i '/^l/{n;d;}' test.txt

# Print lines outside of lines 2-5
[root@localhost ~]# sed -n '2,5!p' test.txt

# Replace all letters 'b' that appear more than once with the number '8' and print them out
[root@localhost ~]# sed -nr 's/b+/8/gp' test.txt

# Delete the content on the 6th line and then change all '*' to '#'
[root@localhost ~]# sed -i -e '5d' -e 's/*/#/g' test.txt

# Create a file file.sed and write the sed script in it, print the lines from the first line to the line ending with '?'
[root@localhost ~]# echo "/1,/^A/p" > file.sed 
[root@localhost ~]# sed -n -f file.sed test.txt 

## Backreference
-- Protect first, then use

# Change "hello world" to "[hello] [world]"
[root@localhost ~]# echo "hello world" | sed -r 's/(\w+)/[\1]/g'
[root@localhost ~]# echo "hello world" | sed -r 's/\w+/[&]/g'

# Replace the first occurrence of a string that appears three times in a row with '嗯哼~'
[root@localhost ~]# sed -r -i 's/([a-z])\1\1/嗯哼~/' test.txt

# Get the IP address of Linux
[root@localhost ~]# ip a s eth0 
9: eth0: <> mtu 1500 group default qlen 1
    link/ether d8:c4:97:92:73:08
    inet 169.254.248.50/16 brd 169.254.255.255 scope global dynamic
       valid_lft forever preferred_lft forever
    inet6 fe80::5219:3165:3e7d:41fb/64 scope link dynamic
       valid_lft forever preferred_lft forever
       
[root@localhost ~]# ip a s eth0 | sed -nr '3s/(^.*t )(.*)(\/.*$)/\2/gp'

# Use sed command to reverse any three lowercase letters, for example: change 'abc' to 'cba'
[root@localhost ~]# echo "abc" | sed -n 's/([a-z])([0-z])(a-z)/\3\2\1/p' 
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.