sed command Full Guide Tutorial | Linux sed Command

sed-command-in-linux

In this blog, you will learn one of the most important commands i.e. sed command. It is mostly used for arranging the output in a readable format which you can easily analyze the data and it is one of the important editors in Linux. So let’s get started with the blog.

Introduction to sed command

sed command” in Linux is a powerful stream editor used for manipulating and transforming text. It reads text input line by line, applies specified operations to each line, and outputs the modified text.

However, this editing is not permanent. Only in appearance; the file’s real content is unchanged. It is mostly used for text replacement, but it can also be used for other text manipulation tasks including inserting, deleting, searching, and more.

We may edit files without opening them using the sed command. It is a more powerful text manipulation tool because it supports regular expressions.

Syntax of the sed command

$ sed OPTIONS 'script' inputfile(s)

Full Guide for SED Command

Let’s see our data.txt file and what content we have stored in the file.

$ cat data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

A) Read a specific line of the data

$ sed -n '3p' data.txt 
2   White   Developer 2000

From the above command, we can clearly see that we want to display the third line of the data. option

-n: Suppresses automatic printing of pattern space.

B) Read the multiple lines of the data

$ sed -n '2,5p' data.txt 
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000

From the above command, we have successfully displayed the line from one to four of the data.

C) Display the last line of the data

$ sed -n '$p' data.txt 
5   Sam   Developer   5000

From the above command, we can clearly see that we want to display the last line of the data. ‘$’ is used to display the last line of the data.

D) Filter the data of the file

$ sed -n '/Tester/p' data.txt 

3   Gill    Tester    3000
4   Lara   Tester     4000

From the above command, we have successfully filtered the data of the “Tester” from the file.

E) Multiple expressions for the sed command

$ sed -n -e '2p' -e '4p' data.txt 
1   Bob   HR         1000
3   Gill    Tester    3000

From the above command, we have successfully displayed only the second & third lie of the data.

$ sed -n -e '/Tester/p' -e '/Developer/p' data.txt 
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have successfully filtered the data of “Tester” & “Developer” in the same command.

F) Print the next nth line from the data

$ sed -n '2,+2p' data.txt 
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000

From the above command, we have successfully displayed the list of the second line and the next two lines successfully.

G) Print the even or odd number of lines of the data

$ sed -n '1~2p' data.txt 
ID  Name  Position  Salary
2   White   Developer 2000
4   Lara   Tester     4000

From the above command, we can easily conclude that 1 stands for start from the first line, and ~2p says that skips every two lines from that.

H) Read the expression from another file with the sed command

Make an express.txt file that stores the expression of the command.

$ cat express.txt 
2p
3p

Here, you can see that, the file contains two expressions that is 2p and 3p which stays that read the 2d and 3rd lines from the data.

$ sed -n -f express.txt data.txt 
1   Bob   HR         1000
2   White   Developer 2000

From the above command, we can easily conclude it reads the express.txt file and display the output according to the expression.

I) Update the word globally in the file.

$ sed 's/Tester/EthicalHacker/g'  data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    EthicalHacker    3000
4   Lara   EthicalHacker     4000
5   Sam   Developer   5000

From the above command, we have substituted the word “Tester” with “Ethical Hacker” successfully.

s/pattern/replacement/flags: Substitutes the first occurrence of a pattern with a replacement string. Flags can be used to modify the behavior (e.g., g for global replacement).

J) Replace the word at a specific line (not globally)

$ sed '4 s/Tester/EthicalHacker/g'  data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    EthicalHacker    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have substituted the word “Tester” with “Ethical Hacker” at line 4 only.

K) Do Not replace the word at a specific line other than that replace globally

$ sed '4! s/Tester/EthicalHacker/g'  data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   EthicalHacker     4000
5   Sam   Developer   5000

From the above command, we have substituted the word “Tester” with “Ethical Hacker” not at line 4, and at the same time it will replace all over the place.

L) Update the data with multiple conditions

$ sed '/Developer/ s/2000/4000/g' data.txt 

ID  Name  Position  Salary
1   Bob   HR         1000
2    White   Developer 4000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have to update the salary with two conditions the first one is “Position must e a Developer” and with the “Salary of 2000” update the salary to 4000.

M) Delete the line from the data

$ sed '3d' data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

Here, you can see that, it deletes the 3rd line from the data and displayed the data.

N) Delete the last line from the data

$ cat data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

$ sed '$d' data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000

Here, you can see that in the first command, you can see all the data of the file, and in the second command it deleted the last line of the file.

O) Delete the Specific data from the file.

Now, I have to delete the User with the position of “Tester” in the file.

$ sed '/Tester/d' data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
5   Sam   Developer   5000

From the above command, we can clearly see that we have successfully deleted the user with the position of “Tester” in the file.

P) Filter out the data and store it in the new file

$ sed '/Tester/ w TestUser.txt' data.txt 

ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

$ ls *.txt

Country.txt   file.txt        'new 3.txt'             results.txt         TestUser.txt

$ cat TestUser.txt 
3   Gill    Tester    3000
4   Lara   Tester     4000

From the above image, the 1st command filters out all the Tester User and writes it into another file “TestUser.txt”. The second command is used for verifying whether the file is present or not. The third command is to check whether the content is stored or not.

Q) Append the data after the specific line

$ sed '4 a  Data Append' data.txt 

ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
Data Append
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have successfully appended the data after the 4th line by using the “a” option in the command.

R) Edit the specific line of the data

$ cat data.txt 

ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

$ sed '4 c Data Append' data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
Data Append
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have successfully modified the 4th line by using the “c” option in the command.

S) Insert the data before the first line in the data

$ sed '1 i Data Inserted' data.txt
 
Data Inserted
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have successfully inserted the data before the first line by using the “i” option in the command.

T) Add an data of external file in the File

$ cat exteral_file.txt 
Data from external File
 
$ sed '3 r exteral_file.txt' data.txt 
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
Data from external File
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have successfully added the data from external in the data.txt file with the help of “r” option.

U) Add the other command output in the file

$ sed '1 e pwd' data.txt 
/home/roger
ID  Name  Position  Salary
1   Bob   HR         1000
2   White   Developer 2000
3   Gill    Tester    3000
4   Lara   Tester     4000
5   Sam   Developer   5000

From the above command, we have successfully added the pwd command’s output in the data.txt file with the help of “e” option.

Regular Expressions with sed command

So this demo file which I made for regular Expressions example you ca also follow it.

$ cat Country.txt 
India
United States
United Kingdom
Canada
ROme
Germany
Netherlands
Iran
Iraq
Russia

A) Read the data which starts with the letter “I”.

$ sed -n '/^I/p' Country.txt 
India
Iran
Iraq

From the above command, we have successfully filter the data which starts from the letter “I” with expression “/^I/p”, where “^” stands for letter starts with particular alphabets

B) Read the data which ends with the letter “a”.

$ sed -n '/a$/p' Country.txt 
India
Canada
Russia

From the above command, we have successfully filter out the data ends with the letter “a” with expression “/a$/p”, where “$” stands for letter ends with particular alphabets.

B) Read the data which comes under the specific range.

$ sed -n '/[A-K]/p' Country.txt 
India
United Kingdom
Canada
Germany
Iran
Iraq

From the above command, we have successfully filter out the data comes under the specific range with expression “/[A-K]/p”, where “[]” is used for the range purpose.

C) Posix Class with sed command

$ cat posix_demo.txt 
RUSSIA
USA
I am new programmer
1234
Hello1234
HELLO1234
Hello, world
this is the demo file.

In this file, we have stored the data like some upper case, lower case, number and the mix of each other.

A) Read only the upper case data from the file

$ sed -n '/[[:upper:]]/p' posix_demo.txt 
RUSSIA
USA
I am new programmer
Hello1234
HELLO1234
Hello, world

From the above command, we have successfully filter out the data which contains upper text in the word by using the double square brackets “[[:upper:]]”, here upper is the class name.

B) Read only the digit data from the file

$ sed -n '/[[:digit:]]/p' posix_demo.txt 
1234
Hello1234
HELLO1234

From the above command, we have successfully filter out the data which contains digits in the word by using the double square brackets “[[:digit:]]”, here digit is the class name.

Recent Articles on Linux

  1. What is Linux Operating System | Introduction to Linux
  2. Directory in Linux Define | Linux Directory & its Commands
  3. Explain the chmod command in Linux | Linux chmod command
  4. Linux User Management || User Management in Linux
  5. Linux Computer Network Advanced Command | Network Command
  6. Redirection in Linux I/O| Linux I/O Redirection
  7. CronTab and Job Scheduling in Linux | Make CronTab Project
  8. Linux Firewall Unlock Rules with Firewall-cmd Tutorial
  9. netstat command in Linux | Linux netstat command
  10. SSH Command Full Guide with Practical | Linux SSH Service
  11. awk command Guide | How to arrange the output of the file in Linux

Recent Articles on CyberSecurity Attacks

  1. 10 Tips for the User to Prevent from Being Hacked by Hackers
  2. Cookie Hijacking, How to Detect and Prevent It with Practicals
  3. Session Hijacking, and How to Detect and Prevent It with Practicals
  4. Social Engineering and its Different Types in CyberSecurity
  5. What is Privilege Escalation Attack, its Types, and Prevention
  6. KeyLogger Attack and How to Detect and Prevent It
  7. Eavesdropping Attack and How to Prevent it in Ethical Hacking
  8. Drive-By Attack and How to Prevent it in Ethical Hacking
  9. Steganography Attack and How to Hide and Send Data in Image
  10. What is SQL Injection, its Type, Prevention, and how to perform it
  11. Broken Access Control Full Guide OWASP 10 in Ethical Hacking
  12. Insecure Deserialization in Ethical Hacking OWASP 10

By Vivek Maurya

Write blogs related to Ethical hacking, Computer networks, Linux, Penetration testing and Web3 Security.

Leave a Reply

Your email address will not be published. Required fields are marked *