Sed Command in Linux

Taranjit Singh Anand
5 min readAug 5, 2022

--

Understanding use of Sed Command (Stream Editor) in Linux, it can perform lots of functions on file like searching, find and replace, insertion or deletion.

syntax : 
$ sed <OPTION> <SCRIPT> <INPUT_FILE>

print

1. Print using Sed command, in the following example we will print line number 3 (‘3p’) from a sample file.

Use the -n option along with the ‘p’ print flag to display only the specified lines

# sed -n ‘3p’ sed_linux.txt
printing a specific line

2. Now, lets try to print range of line number 1 to 3 (‘1,3p’) from the sample file.

# sed -n ‘1,3p’ sed_linux.txt
print line from 1 to 3 using Sed command
print line from 1 to 3 using Sed command

3. Now, to print line number 1 and 4 (‘1p;4p’) from the sample file.

# sed -n ‘1p;4p’ sed_linux.txt
print line number 1 and 4

4. To print the last line (‘$p’).

# sed -n ‘$p’ sed_linux.txt 
print last line

5. We can also use sed on other command using pipe, here we will print range of line number 2 to 7 of lscpu’s output with line number.

# lscpu | cat -n | sed -n ‘2,7p’

6. To redirect output of the above operation to a file, utilize the ”>” and ”>>” redirection operators.

save the output

Find & Replace and Print

7. Find & Replace text and Print : find ‘CPU’ and replace it with ‘Processor’, and print the output ( ‘s/CPU/Processor/’).

NOTE : It will find and replace only the first occurrence, won’t cause any change in the target file.

$ sed ‘s/CPU/Processor/’ lscpu_output.txt “s” specifies the substitution operation
“/” are delimiters

8. Find & Replace text (global, all occurrences) and Print : find ‘CPU’ and replace it with ‘Processor’, and print the output ( ‘s/CPU/Processor/’).

$ sed ‘s/CPU/Processor/g’ lscpu_output.txt “s” specifies the substitution operation
“/” are delimiters
"g" (global replacement) specifies the sed command to replace all the occurrences of the string in the line.

9. Find & Replace text in a specified line and Print : find ‘CPU’ and replace it with ‘Processor’, in the line number 7 and print the output (‘7s/CPU/Processor/’).

$ sed ‘7s/CPU/Processor/g’ lscpu_output.txt7s specifies the substitution is to be performed on line number 7

10 . Find & Replace text multiple lines and Print : find text ‘Installed’ and replace it with ‘Deployed’ in range of line 4 to 9 in the file.

$ cat -b aug_log.txt | sed ‘4,9s/Installed/Deployed/g'4,9s specifies the substitution is to be performed from line number 4 to 9. 

11 . Find & Replace 2 keywords and Print : find the keywords ‘Installed’ & ‘Jun’ & replace them with ‘Deployed’ & ‘May’ respectively, in the file.

$ cat -b aug_log.txt | sed ‘s/Installed/Deployed/g ; s/Jun/May/g'

12. Find & Replace text in target file : find text ‘Installed’ and replace it with ‘Deployed’ in range of line 4 to 9 in the file.

# sed -i ‘s/Mon/Fri/g’ new_job.txtIf you use the "-i" option the sed command replaces the text in the original file itself rather than displaying it on the terminal.Note: Be careful in using the "-i" option. Once you changed the contents of the file, you cannot revert back to the original file. It is good to take the backup of the original file.

13. Commenting out line number 4 in a file using sed command.

# sed -i ‘4s/^/#/g’ test.sh'i' will change the text in test.sh file.
'4s' specifies that the action is to done on line number 4.
'^' It means "beginning of the line."
'/^/#/' together they mean, add '#' in the beginning of the line, which will eventually comment out the line.

Delete

14. Deleting lines. SED command can also be used for deleting lines from a particular file. SED command is used for performing deletion operation without even opening the file.

syntax:
# sed -i ‘nd’ new_job.txt
'n' is nth line number that you want to delete
'-i' use '-i' flag to delete line/s from original file
# sed -i '5d' new_job.txt
'5d' will delete line number 5.
# sed -i '2,4d' new_job.txt
'2,4d' will delete line from range 2 to 4

15. Deleting all lines, excluding the nth line, and save the output to a new file.

# sed '5!d' new_job.txt > new_info.txt
exclude line 5, and delete all the other lines and save the output.
# sed '2,7!d' new_job.txt > new_info.txt
exclude range from line 2 to 7, and delete all the other lines and save the output.

--

--