As a Linux user, understanding the power of command-line tools is essential for efficient system management and text manipulation. Among the various tools available, Sed (Stream Editor) stands out as a versatile utility that allows to transform, modify, and manipulate text files effortlessly.
In this blog post, we will explore 20 practical sed command examples that will empower you to perform complex text editing tasks with ease. So, let’s dive into the world of sed and unlock its potential!
Syntax
# sed OPTIONS… [SCRIPT] [INPUTFILE…]
Now let’s see some examples.
1) Print Specific Lines
With sed, we can print specific lines of a file rather than seeing whole file. So, to print specific lines of a file, use the following command,
$ sed -n 22,29p testfile.txt
here, option ‘n’ suppresses printing of whole file & option ‘p’ will print only line lines from 22 to 29.
2) Print all except some lines
To print all content of a file except for some portion, use the following command,
$ sed 22,29d testfile.txt
Option ‘d’ will remove the mentioned lines from output.
3) Display every 3rd line starting with Nth line
Do display content of every 3rd line starting with line number 2 or any other line, use the following command
$ sed -n '2~3p' file.txt
Insert Text Before a Specific Line:
$ sed '4i\inserted-text' filename
Append Text After a Specific Line:
$ sed '4a\appended-text' filename
4 ) Deleting a line using sed command
To delete a line with sed from a file, use the following command,
$ sed Nd testfile.txt
where ‘N’ is the line number & option ‘d’ will delete the mentioned line number. To delete the last line of the file, use
$ sed $d testfile.txt
5) Deleting a range of lines
To delete a range of lines from the file, run
$ sed '29,34d' testfile.txt
This will delete lines 29 to 34 from testfile.txt file.
Deleting all empty lines:
In order to delete all the empty or blank lines from a file, use the following command:
$ sed '/^$/d' filename
6) Deleting lines other than the mentioned
To delete lines other than the mentioned lines from a file, we will use ‘!’
$ sed '29,34!d' testfile.txt
here ‘!’ option is used as not, so it will reverse the condition i.e. will not delete the lines mentioned. All the lines other 29-34 will be deleted from the files testfile.txt.
7) Adding Blank lines
To add a blank line after every non-blank line, we will use option ‘G’,
$ sed G testfile.txt
Replace Tabs with Spaces:
$ sed 's/\t/ /g' filename
8) Search and Replae a string
To search & replace a string from the file, we will use the following example,
$ sed 's/danger/safety/' testfile.txt
here option ‘s’ will search for word ‘danger’ & replace it with ‘safety’ on every line for the first occurrence only.
9) Search and Replace a string from whole file
To search and replace a string globally from the file, we will use option ‘g’ with ‘s’ as shown in the example below,
$ sed 's/danger/safety/g' testfile.txt
10) Replace the nth occurrence of string pattern
We can also substitute a string on nth occurrence from a file. Like replace ‘danger’ with ‘safety’ only on second occurrence,
$ sed ‘s/danger/safety/2’ testfile.txt
To replace ‘danger’ on 2nd occurrence of every line from whole file, use
$ sed 's/danger/safety/2g' testfile.txt
11) Substitute Text on a Specific Line
To search and replace a string on the specific line, use below command
$ sed '4 s/danger/safety/' testfile.txt
This will only substitute the string from 4th line of the file. We can also mention a range of lines instead of a single line,
$ sed '4,9 s/danger/safety/' testfile.txt
12) Add a line after/before the matched search
To add a new line with some content after every pattern match, use option ‘a’ ,
$ sed '/danger/a "This is new line with text after match"' testfile.txt
To add a new line with some content a before every pattern match, use option ‘i’,
$ sed '/danger/i "This is new line with text before match" ' testfile.txt
13) Change a whole line with matched pattern
To change a whole line to a new line when a search pattern matches we need to use option ‘c’ with sed,
$ sed '/danger/c "This will be the new line" ' testfile.txt
So when the pattern matches ‘danger’, whole line will be changed to the mentioned line.
Advanced options with sed
Up until now we were only using simple expressions with sed, now we will discuss some advanced uses of sed with regex,
14) Running multiple sed commands
If we need to perform multiple sed expressions, we can use option ‘e’ to chain the sed commands,
$ sed -e 's/danger/safety/g' -e 's/hate/love/' testfile.txt
15) Making a backup copy before modifying a file
To create a backup copy of a file before we edit it, use option ‘-i.bak’,
$ sed -i.bak -e 's/danger/safety/g' testfile.txt
This will create a backup copy of the file with extension .bak. You can also use other extension if you like.
16) Delete a file line starting with & ending with a pattern
To delete a line starting with a particular string & ending with another string, use
$ sed -e 's/^danger.*stops$//g' testfile.txt
This will delete the line with ‘danger’ on start & ‘stops’ in the end & it can have any number of words in between , ‘.*’ defines that part.
Delete Leading Whitespace:
$ sed 's/^[[:space:]]*//' filename
Remove Trailing Whitespace:
$ sed 's/[[:space:]]*$//' filename
17) Appending lines
To add some content before every line with sed & regex, use
$ sed -e 's/.*/testing sed &/' testfile.txt
So now every line will have ‘testing sed’ before it.
18) Removing all commented lines, empty line and duplicate lines
To remove all commented lines i.e. lines with # & all the empty lines, use
$ sed -e 's/#.*//;/^$/d' testfile.txt
To only remove commented lines, use
$ sed -e 's/#.*//' testfile.txt
Remove Duplicate Lines:
$ sed '$!N; /^\(.*\)\n\1$/!P; D' filename
19) Get list of all usernames from /etc/passwd
To get the list of all usernames from /etc/passwd file, use
$ sed 's/\([^:]*\).*/\1/' /etc/passwd
a complete list all usernames will be generated on screen as output.
20) Prevent overwriting of system links with sed command
‘sed -i’ command has been known to remove system links & create only regular files in place of the link file. So to avoid such a situation & prevent ‘sed -i‘ from destroying the links, use ‘–follow-symklinks‘ options with the command being executed.
Let’s assume i want to disable SELinux on CentOS or RHEL Severs
$ sudo sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
These were some examples to show sed, we can use these reference to employ them as & when needed.
Conclusion:
Congratulations on reaching the end of this comprehensive Sed command examples guide! You have acquired a solid foundation in using Sed to manipulate and transform text in Linux. Remember, Sed is a powerful tool that can simplify complex text editing tasks and improve your productivity as a Linux user. So, go ahead and experiment with these examples, unleash your creativity, and master the art of text manipulation with Sed!.
If you guys have any queries related to this guide, kindly post your queries in below comments section.
Also Read: 14 SCP Command Examples to Securely Transfer Files in Linux
example for Remove the nth occurrence of string pattern???
Thanks. I’ve a question. Wondering if you can help me with this.
How do I append text
from
acceptCount=”100″ disableUploadTimeout=”true” bindOnInit=”false”
to
acceptCount=”100″ disableUploadTimeout=”true” bindOnInit=”false” proxyName=”jira.stg-tle.dps” proxyPort=”443″ scheme=”https”
range of lines is , in sed toool and not -. Examples 1,2,3,5 and 6 need update.
Use tilde ~ instead of – in example 3
Rest all very useful
Thank You Rod for identifying the typo, i have corrected the suggested examples in article.