sed tutorial: sed address and address range(select lines) with examples

The sed command address and address range are the most commonly used functions in our daily use.

By using addresses or address ranges, the sed command can apply certain actions to these address lines.

The following text content is the content of our test file, we will use the following content as an example.

1. This is a test file.
2. sed address and address range
3. sed action at a specific address or address range.
4. Sed action options: p, g, d ...
5. sed stream
6. sed negating address range

Sed performs actions on specific line (addresses)

In the following example, we will use the sed command to perform print or delete pattern space operations on a specific line (address).

Print the second line of the test file with the sed command

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

Print the last line of the test file with the sed command

➜  ~ sed -n '$p' test.txt

Delete the second line of the test file with the sed command

➜  ~ sed '2d' test.txt

Insert a new line before the second line with the sed command

➜  ~ sed '2 i Insert a new line before the second line' test.txt

Insert a new line after the second line with the sed command

➜  ~ sed '2 a Insert a new line before the second line' test.txt

Sed performs actions on specific lines range (address range)

In the following example, we will use the sed command to perform print or delete pattern space operations on a specific lines range (address range).

Print the second to fourth lines of the test file with the sed command

➜  ~ sed -n '2,4p' test.txt

Print the second and fourth lines of the test file with the sed command

➜  ~ sed -n '2p;4p' test.txt

Delete the last line of the test file with the sed command

➜  ~ sed '$d' test.txt

sed performs actions on lines that match the regular expression pattern

In the following example, we will use the sed command to match some specific line addresses through regular patterns, and then print or delete the pattern space.

Regular pattern matches lines starting with “2” and prints

➜  ~ sed -n '/^2/p' test.txt
2. sed address and address range

Regular pattern matches the line containing “address” string and prints

➜  ~ sed -n '/address/p' test.txt
2. sed address and address range
3. sed action at a specific address or address range.
6. sed negating address range

Regular pattern matches the lines address range that starts with the number “2” or contains the string “options” and deletes

➜  ~ sed '/^2/d;/options/d' test.txt

Sed command performs actions on the lines address range specified by numbers and patterns

In the following example, we will use the sed command to perform a print or delete action on the line address range where the specific line number and regular pattern match.

Print the second line and the line containing the string “option”

➜  ~ sed -n '2p;/option/p' test.txt
2. sed address and address range
4. Sed action options: p, g, d ...
➜  ~

Delete the address range of the regular pattern matching to the last line

➜  ~ sed '/4/, $d' test.txt
1. This is a test file.
2. sed address and address range
3. sed action at a specific address or address range.

Sed command performs actions in the negating address range

In the following example, we will use the sed command to perform a print or delete operation on the negating address range.

Print lines that are not in the 2 to 4 line address range

➜  ~ sed -n '/[2-4]/!p'  test.txt
1. This is a test file.
5. sed stream
6. sed negating address range
➜  ~

Delete the line that is not the last line of address range

➜  ~ sed '$!d' test.txt
6. sed negating address range
➜  ~

Add a Comment

Your email address will not be published.