Use of sed command

Keywords: Mac OS X Linux CentOS

Sed is a stream editor. It is a very useful tool in text processing. It can perfectly cooperate with regular expressions and has extraordinary functions. When processing, the currently processed rows are stored in a temporary buffer called pattern space, and then the contents of the buffer are processed with sed command. After processing, the contents of the buffer are sent to the screen. The next line is processed and repeated until the end of the file. The content of the file remains unchanged unless you use redirection to store the output. Sed is mainly used to edit one or more files automatically, simplify the repeated operation of files, write conversion programs, etc.

sed's Options, Commands, Replacement Markers

Command format

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

option

parameter Complete parameter Explain
-e script --expression=script Processing input text files with the specified script in the option
-f script --files=script Processing input text files with the specified script file in the option
-h --help Display help
-n --quiet --silent Show only the results after script processing
-V --version display version information

parameter

File: Specify a list of text files to be processed

sed command

command Explain
d Delete, delete selected rows
D Delete the first line of the template block
s Replace the specified character
h Copy the contents of the template block to the buffer in memory
H Append the content of the template block to the buffer in memory
g Get the contents of the memory buffer and replace the text in the current template block
G Get the contents of the memory buffer and append it to the back of the current template block text
l List cannot print a list of characters
n Read the next input line and process the new line with the next command instead of the first command
N Add the next input line to the back of the template block and insert a new line between them to change the current line number
p Lines of Print Template Blocks
P The first line of the print template block
q Quit sed
b label Branch to the tagged part of the script, or to the end of the script if the branch does not exist
r file Read from file
t label if the branch starts at the last line, once the condition is satisfied or the T, t command is met, it will lead to the branch to the labeled command or to the end of the script.
T label Error branch, starting at the last line, will cause branch to the labeled command or to the end of the script once an error or T, t command occurs.
w file Write and append the template block to the end of the file
W file Write and append the first line of the template block to the end of the file
! Represents that the following command acts on all unselected rows
= Print the current line number
# Extend the comment before the first newline character

sed replacement tag

command Explain
g Represents in-line full replacement
p Represents a print line
w Represents writing rows to a file
x Represents text in an interchange template block and text in a buffer
y Represents the translation of a character into another character (but not for regular expressions)
\1 Substring matching tag
& Matched String Markup

sed metacharacter set

command Explain
^ Match rows start, such as: /^ sed / Match all rows that start with sed.
$ Match line ends, such as: / sed$/ Match all lines ending with sed.
. Match any character that is not a newline character, such as: / s.d/ match s followed by an arbitrary character, and finally D.
* Match 0 or more characters, such as: /* sed / Match all templates with one or more spaces followed by sed lines.
[] Match a character within a specified range, such as /[sS]ed/match sed and sed.
[^] Match a character that is not in the specified range, such as: /[^ A-RT-Z]ed / Match a line that does not contain a letter beginning of A-R and T-Z, followed by ed.
(..) Match substrings, save matched characters, such as s/(love)able/1rs, love able is replaced by lovers.
& Save search characters to replace other characters, such as s/love/&/, love.
< Match the beginning of the word, such as: /<love/Match the line containing the word beginning with love.
 > Match the end of the word, such as / love >/ match the line containing the word ending with love.
x{m} Repeat the character x, m times, such as: / 0 {5}/ match lines containing 5 0.
x{m,} Repeat the character x at least m times, such as: / 0 {5,}/ match at least five lines of 0.
x{m,n} Repeat the character x, at least m times, no more than n times, such as: /0{5,10}/Match lines from 5 to 10 0.

Examples of sed usage

Let's prepare a test file first.

MacBook-Pro:tmp maxincai$ cat test.txt
my cat's name is betty
This is your dog
my dog's name is frank
This is your fish
my fish's name is george
This is your goat
my goat's name is adam

Replacement operation: s command

Replace strings in text:

MacBook-Pro:tmp maxincai$ sed 's/This/aaa/' test.txt
my cat's name is betty
aaa is your dog
my dog's name is frank
aaa is your fish
my fish's name is george
aaa is your goat
my goat's name is adam

- The n option is used with the p command to indicate that only those lines that have been replaced are printed:

MacBook-Pro:tmp maxincai$ sed -n 's/This/aaa/p' test.txt
aaa is your dog
aaa is your fish
aaa is your goat

During the testing process, it was found that mac os x and linux were still a little different, and they were replaced by centos 6.5 for testing.

The direct edit file option - i matches the first This of each line in the test.txt file and replaces it with this:

[root@vagrant-centos65 workspace]# sed -i 's/This/this/' test.txt
[root@vagrant-centos65 workspace]# cat test.txt
my cat's name is betty
this is your dog
my dog's name is frank
this is your fish
my fish's name is george
this is your goat
my goat's name is adam

Complete Substitution Marker g

Using the suffix / g tag replaces all matches in each row:

[root@vagrant-centos65 workspace]# sed 's/this/This/g' test.txt
my cat's name is betty
This is your This dog
my dog's name is This frank
This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam

When you need to start replacing from matching at N, you can use / Ng:

[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's/sk/SK/2g'
skSKSKSKSKSK
[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's/sk/SK/3g'
skskSKSKSKSK
[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's/sk/SK/4g'
skskskSKSKSK

Delimiter

Characters / SEDS in the above commands can be u sed as delimiters or arbitrary delimiters:

[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's:sk:SK:4g'
skskskSKSKSK
[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's|sk|SK|4g'
skskskSKSKSK

When delimiters appear inside a style, they need to be escaped:

[root@vagrant-centos65 workspace]# echo '/usr/local/bin' | sed 's/\/usr\/local\/bin/\/USR\/LOCAL\/BIN/g'
/USR/LOCAL/BIN

Delete operation: d command

Delete blank lines:

[root@vagrant-centos65 workspace]# cat test.txt
my cat's name is betty

this is your this dog

my dog's name is this frank

this is your fish

my fish's name is this george

this is your goat

my goat's name is this adam

[root@vagrant-centos65 workspace]# sed '/^$/d' test.txt
my cat's name is betty
this is your this dog
my dog's name is this frank
this is your fish
my fish's name is this george
this is your goat
my goat's name is this adam

Delete line 2 of the file:

[root@vagrant-centos65 workspace]# sed '2d' test.txt
my cat's name is betty
my dog's name is this frank
this is your fish
my fish's name is this george
this is your goat
my goat's name is this adam

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

[root@vagrant-centos65 workspace]# sed '2,$d' test.txt
my cat's name is betty

Delete the last line of the file:

[root@vagrant-centos65 workspace]# sed '$d' test.txt
my cat's name is betty
this is your this dog
my dog's name is this frank
this is your fish
my fish's name is this george
this is your goat

Delete all lines in the file that begin with my:

[root@vagrant-centos65 workspace]# sed '/^my/'d test.txt
this is your this dog
this is your fish
this is your goat

Matched String Markup&

The regular expression \w\+matches each word, replaces it with [&], & corresponds to the previously matched word:

[root@vagrant-centos65 workspace]# echo this is a test line | sed 's/\w\+/[&]/g'
[this] [is] [a] [test] [line]

Substring Matching Marker\1

Match part of a given style:

[root@vagrant-centos65 workspace]# echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number

Diit 7 in the command is replaced by 7. Style matched substrings are 7, (.. \) used to match substrings. For the first substring marked 1, the second result of matching is 2 by analogy, for example:

[root@vagrant-centos65 workspace]# echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa

Combining multiple expressions

Sed'expression'| sed'expression'

Equivalent to

sed'expression; expression'

Quote

sed expressions can be referenced with single quotes, but if the expression contains a variable string inside, double quotes are required.

[root@vagrant-centos65 workspace]# test=hello
[root@vagrant-centos65 workspace]# echo hello WORLD | sed "s/$test/HELLO/"
HELLO WORLD

Range of selected rows: (comma)

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

[root@vagrant-centos65 workspace]# sed -n '5,/^this/p' test.txt
my fish's name is this george
this is your goat

Multipoint Editing: e Command

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

[root@vagrant-centos65 workspace]# sed -e '1,5d' -e 's/my/MY/' test.txt
this is your goat
MY goat's name is this adam

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 commands are executed affects the results. If both commands are replacement commands, the first command will affect the result of the second command.

The command equivalent to - e is -- expression

Read from the file: r command

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

[root@vagrant-centos65 workspace]# cat test1.txt
aaaaaaaa
[root@vagrant-centos65 workspace]# sed '/my/r test1.txt' test.txt
my cat's name is betty
aaaaaaaa
this is your this dog
my dog's name is this frank
aaaaaaaa
this is your fish
my fish's name is this george
aaaaaaaa
this is your goat
my goat's name is this adam
aaaaaaaa

Write file: w command

All rows containing my in test.txt are written to test2.txt:

[root@vagrant-centos65 workspace]# sed -n '/my/w test2.txt' test.txt
[root@vagrant-centos65 workspace]# cat test2.txt
my cat's name is betty
my dog's name is this frank
my fish's name is this george
my goat's name is this adam

Additional (under line): a Order

Append this is a test line to the line beginning with my:

[root@vagrant-centos65 workspace]# sed '/^my/a\this is a test line' test.txt
my cat's name is betty
this is a test line
this is your this dog
my dog's name is this frank
this is a test line
this is your fish
my fish's name is this george
this is a test line
this is your goat
my goat's name is this adam
this is a test line

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

[root@vagrant-centos65 workspace]# sed '2a\this is a test line' test.txt
my cat's name is betty
this is your this dog
this is a test line
my dog's name is this frank
this is your fish
my fish's name is this george
this is your goat
my goat's name is this adam

Insert (on line): i command

Insert this is a test line in front of the line starting with my:

[root@vagrant-centos65 workspace]# sed '/^my/i\this is a test line' test.txt
this is a test line
my cat's name is betty
this is your this dog
this is a test line
my dog's name is this frank
this is your fish
this is a test line
my fish's name is this george
this is your goat
this is a test line
my goat's name is this adam

Next: n command

If my is matched, move to the next line of the matched line, replace this line with This, and print the line:

[root@vagrant-centos65 workspace]# sed '/my/{n; s/this/This/; }' test.txt
my cat's name is betty
This is your this dog
my dog's name is this frank
This is your fish
my fish's name is this george
This is your goat
my goat's name is this adam

Deformation: y command

Convert all abcde s in 1-10 lines to uppercase. Note that regular expression metacharacters cannot use this command:

[root@vagrant-centos65 workspace]# sed '1,10y/abcde/ABCDE/' test.txt
my CAt's nAmE is BEtty
this is your this Dog
my Dog's nAmE is this frAnk
this is your fish
my fish's nAmE is this gEorgE
this is your goAt
my goAt's nAmE is this ADAm

Exit: q command

Print line 3 and exit sed

[root@vagrant-centos65 workspace]# sed '3q' test.txt
my cat's name is betty
this is your this dog
my dog's name is this frank

Print odd or even lines

Method 1:

Odd row

[root@vagrant-centos65 workspace]# sed -n 'p;n' test.txt
my cat's name is betty
my dog's name is this frank
my fish's name is this george
my goat's name is this adam

Even number line

[root@vagrant-centos65 workspace]# sed -n 'n;p' test.txt
this is your this dog
this is your fish
this is your goat

Method 2:

sed -n '1~2p' test.txt
sed -n '2~2p' test.txt

More needs to be explored slowly in the future work, here is only a simple record, if there is more experience in the future to improve a sed actual combat bar.

Posted by ricmetal on Wed, 15 May 2019 09:26:12 -0700