Are you a Linux system administrator?
Are you a Linux System engineer?
Are you seeking a job in Linux system administrator?
Do you want to learn Linux Operating System?
Do you want to work as a Linux Professional or expert?
If your answer is “Yes” for any above question, you must have to know to do multiple operating with text file without opening the files.
As we know, Linux is a file-based operating system and file is everything for Linux.
In this case, either your working profile in Linux or you passionate to work with Linux; you should have SED command knowledge which will make your daily work easier.
Today in this article, I will give you 15 best examples of SED command to use in your daily task.
First, if you are not familiar with this command or you have heard the first time about SED command, I will suggest you read my previous article on SED command “Linux SED command help and example”.
It will help you to understand the objective of the SED command and how to use this command with correct syntax and options.
Suggested Reading – Linux SED command Help and Examples
Now, I hope you have already familiar with uses of SED command we can start the best 15 examples of SED commands.
Let’s start the examples for SED command which will change your Linux working day-to-day activities from after reading and practices of this article.
Display given range of lines from a documents
Using SED command to view only some part of file content in the place for the whole file, and this par has defined with line numbers.
The below command will display one eight lines of content from file linuxconcept.txt file which contain the content from line 7 to 14.
The option “n” is to suppress whole printing file, and “p” is use for printing the mentioned line numbers.
Display the entire file content except for the given range of lines
We can also use SED command to view all content except some part of the content, which is also defined by line numbers.
Below command will display the whole content of file linuxconcept.txt except line 7 to 14. The option “d” help to remove mentioned line numbers from the output.
Display non-consecutive lines and ranges
There is also a possibility with SED command to print the non-consecutive lines from a file or more than one range of lines from a file.
Here in below command, let’s display the line 5-8 and 15-20 from the file linuxconcept.text.
The “-e” option is used to execute the given action; in the above command, it is to print 5-8 and 15-20 lines.
Display every 5th line starting with Nth line
To display every 5th line of a file starting from line number 3, we can use SED command in below format.
Delete specific line in the file without opening the file
To delete any specific line from a text file with opening file, we can use below command
The option “d” is used to delete the line from a file and “N” is used to specify the number to remove from file.
Like if you want to delete line number 7 from the file linuxconcept.txt, use the below command
To delete the last line of the file using below command
Delete a range of lines in the file without opening the file
To delete the range of lines from a file specify the range in command and run like below SED command
The above command will delete lines 20 to 30 from file linuxconcept.txt.
Delete all lines in a file other than the mentioned
You can also delete all lines from a file other than mentioned line. Sometimes you get whole file’s content are not essential, but some line of text is necessary to keep, in that case, you can delete file content except those essential lines.
You can do this operation by using below sed command
The above command will delete all content from file linuxconcept.txt except the line 20 to 30.
Replacing word or character in a file
The is the primary use of sed command to replace the pattern, character, word or any string.
To search and replace pattern of string from a file use the below command
This command will search the string “Linux” and replace it with “UNIX. The above command will only replace the first occurrence of the string in line.
The option “s” is used to search the pattern or string.
To search and replace all occurrence in the file, use the above command with “g” global option like below command.
Replacing word or character in given range of lines in a file
You can also use search and replace string for a given range of lines instead of the whole file.
To do this use command with specifying the range of line, like below command
The above command searches all occurrence of string “Linux” in line 20 to 30 and replaces it with “UNIX”.
Replacing word or character using regular expression
You can also use regular expression in sed command, like here I used a regular expression to specify the name of the searching pattern and replacing it.
The above command will search all occurrence of zipping and zip and replace with “raw”. Here I use a regular expression to search string with both upper and lower case of character “Z”
Display line contains the given pattern
You can also use the sed command to print all line from a file which contains the pattern.
Here, in below command, I print all lines from file linuxconcept.txt which are starting with word “Linux”.
Inserting blank lines/spaces
In a text file, to insert a blank line after every non-blank line uses the below command.
The option “G” is used to insert a blank line in the file.
If you want to use more blank line use the same option “G” multiple time separate with a semicolon, like below command
Replacing word only if separate pattern match found
You have already read the command to search and replace string above, but if you have a scenario like you have to search some string and if found then again search another string in the same line and replace.
Yes, it is also possible with below command, where we search a string pattern “OS” and if found then again search “Linux” in the same line and replace it with “UNIX”.
Do two or more substitutions in a single command
Yes, you can use two string substitution in a single command with the sed command.
The below command is used to search “Linux” and replace with “UNIX”, and the same time it also searches for “operating system” and replace it with “OS”.
Making a backup copy before editing a file
When you are changing any file with sed command, you can make a backup copy also for original file in a same single command.
To do this use “-i.bak” option with sed command like below:
Here in this article, we have shared 15 most helpful tips for sed command in System administrator’s day-to-day life. If you think we missed some tips or you use other tricks also in daily task, please share in the comment section.