1. Introduction


Sed is a stream editor that processes content one line at a time. It can perform lot’s of operations over a file like find and replace, insertion or deletion in a linux/unix environment. In this article you will learn about SED command with some examples helpful for beginners.

2. Sed Invoke method


Sed can be invoked in two forms.

Option 1: sed [options] -f scriptfile file(s)
Option 2: sed [options] 'command' file(s)

The first option writes the command in the script. The second option is executed directly on the command line. There is no essential difference between the two.

Example (3): Print the content of test.txt

sed -n p test.txt

Description

  1. -n: sed will print out the text to be processed before processing a line of text, the -n parameter turns off this function
  2. p: command to print the current line
  3. test.txt: file to be processed

This instruction is equivalent to cat

3. Addressing


Input sed, the line you expect to process, represented by two parameters separated by commas. In that first parameter will be the start line number and the second parameter will be end number.

Of course, you can also use regularization to locate the desired rows.

Example (3.a): Print the Third line to last line of hello.txt

sed -n '3,$'p hello.txt

Example (3.b): Print the line that matches “hello” in test.txt

sed -n '/hello/'p hello.txt

Note: $ sign represents the last line;

4. Basic commands


The content of hello.txt is

1 2 3
4 5 6 
7 8 9 10 11

Delete Command: d\


Deletes all the lines from the pattern buffer. We can also instruct to perform this operation only on certain lines.

Example (4.a) Delete the 2nd line 

sed '2d' hello.txt
The output content is:

1 2 3
7 8 9 10 11

Append Command: a\


Add a line of text after the matched line

Example (4.b) Match the line of 7, and add a line “new line” after it

sed '/7/'a\ "new line" hello.txt
The output content is:
1 2 3
4 5 6 
7 8 9 10 11
new line

Insert Command: i \


Add a line of text before the matching line

Example (4.c) Match “7” lines, add a line “new line” in front

sed '/7/'i\ "new line" hello.txt
The output content is:
1 2 3
4 5 6 
new line
7 8 9 10 11

Change Command: c\


Replace matching lines with destination lines

Example (4.d) Match “7” lines, replace with “new line”

sed '/7/'c\ "new line" hello.txt
The output content is:
1 2 3
4 5 6 
new line

Command: s


Replace matching lines

The detailed command is: s/pattern-to-find/replacement-pattern/g

  1. pattern-to-find: the string to be replaced
  2. replacement-pattern: Replace with this string
  3. g: Replace all, by default only replace the first match

Example (4.e) Say “7” is replaced by hello

sed 's/7/hello/g' hello.txt
The output content is:
1 2 3
4 5 6 
hello 8 9 10 11

5. Standard Regular Expressions (Regex)


  • (^) match the beginning of a line.
  • ($) match the end of a line.
  • (.) Match a character.
  • [abc] matches the specified range of characters.
  • [^] Negates the set of characters in the square brackets.
  • [-]  matches any character within the range specified in square brackets
  • (*) zero or more occurrence of the preceding character.

6. Useful commands


Match the line starting with 7, replace it with true, and output

sed -n 's/^7/true/p' hello.txt
The output content is
1 2 3
4 5 6 
true 8 9 10 11

Conclusion


This is a small introduction about SED and basic commands to start with your daily system administration tasks. Refer to this master guide for detailed reference. I would love to hear from you about other tips and commands which you are using on a regular basis. Feel free to comment below.