Sed tutorial Introduction
This sed tutorial is a part of the Sed command in UNIX with examples article.
Sed is a very powerful stream editing tool. In this tutorial we will cover the most common and important usage of sed tool for everyday Linux administration. It is advisable to learn and practice each sed examples given in this sed tutorial to get hands-on the sed command.
You will find that all the given sed examples are used for daily stream editing tasks in Linux and UNIX.
Most common usage of the tool
Other important uses for Linux administration
- Checking log files
- Cleaning config files
Note: Do not use '-i' option , use it only if you are sure and happy with the output. Because it makes the permanent changes to the file.
Option 'p' in sed is used for priniting.
Display what is inside the file
Display the whole content of the file by the following sed command.
$ sed -n 'p' sed_file.txt
Sed print nth line
Display Line number 2 with this command.
$ sed -n '2p' sed_test.txt
Simillarly to display line number 3,4 and 5 use 3p,4p and 5p respectively.
Sed print line range
Display range of lines from 2 to 4 with the given sed command.
$ sed -n '2,4p' sed_test.txt Line 2 Line 3 LIne 4
Display lines from 4 to 6.
$ sed -n '4,6p' sed_test.txt LIne 4 Line 5 Line 6
Sed print multiple range of lines
Display multiple range of lines at a time. For example to display lines from 2 to 4, 1 to 3 and 5 to 6 at the same time, use the below given sed example.
$ sed -n -e '2,4p' -e '1,3p' -e '5,6p' sed_test.txt
Sed count lines in your file
The below command actually count the last line number in the file.
$ = Represents last line in the file.
$ sed -n '$=' sed_test.txt
Sed display line number with its sentence
This will display all the lines with its line numbers.
$ sed '=' sed_test.txt
Display line numbers of matching pattern
$ sed -n '/brown/,/example/=' sed_test.txt
Insert spaces between lines
You can use sed command to insert spaces between lines. The option capial 'G' is used to insert space between lines.
$ sed G sed_test.txt
Similarly, to insert two spaces between lines, use 'G;G' and for three spaces 'G;G;G'
$ sed 'G;G' sed_test.txt
Sed replace string in file
The below sed command will show you how to replace any string in a file. The below sed command will search and replace the string "Line" with "Line number".
$ sed 's/Line/Line number/g' sed_test.txt
Sed replace multiple spaces with one
If multiple blank spaces occurs between the words in a file, replace it with a single space by the following sed command.
$ sed 's/ */ /g' sed_test.txt
Sed command to change case
With sed command you can change the case of letters lower case to upper case and vice versa.
To change the words from lower case to upper case use the following sed command.
$ sed -e 's/\(.*\)/\U\1/' sed_test.txt
Similarly to change from upper to lower case use the following.
$ sed -e 's/\(.*\)/\L\1/' sed_test.txt
Replace letter to uppercase after period
You can use sed command to uppercase first letter of the first word in a file or that comes after a period (.). See the below example.
$ sed -E 's/(^[a]|\. [a-z])/\U&\E/g' sed_test.txt
The below command will append the string 'and black' after each matched string 'brown'.
$ sed 's/\(brown \)/\1 and black /' sed_test.txt
Appending after given line number
Add string after particular line. The following command will add a sentence "New line" after line number 1. Simmilarly you can add new sentence after line number 2, 3 or 4.
$ sed '1 a\New line' sed_test.txt
Appending at the beginning of line
To append sentence at the beginning of line.
$ sed '1s/^/Beginning of line\n/' sed_test.txt
Appending at the end of the line
To append sentence at the ending of line.
$ sed '$ a\Ending of line' sed_test.txt
Sed delete lines (contents)
If you want to delete the whole content of any file, consider using the below sed UNIX command.
$ sed 'd' sed_test.txt
The above command will delete the whole content of the file, but yes you would say there are other commands available in Linux and UNIX which can be used for the same purpose. Some examples are the following.
# echo -n "" > filename
# cp /dev/null filename
# > filename
# cat /dev/null > filename
# dd if=/dev/null of=filename
All the above commands do the same thing, that is erasing the contents of the file. In reality it is just copying null to your file.
I have no rivalries against using the above given commands, you can use it for practice if you want.
But what if you want to delete second line, third line or fourth line from the file? In that case use the sed command in a simple way as given below.
$ sed '1d' filename //Deleting first line
$ sed '2d' filename //Deleting second line
$ sed '3d' filename //Deleting third line
$ sed '4d' filename //Deleting fourth line
$ sed '3,6d' filename //Deleting lines 3 to 6
I have added two other and most important use of sed command for stream editing task in Linux and UNIX.
Checking log files
Every Linux administrator has to check log files every day. This can be a very time consuming activity as these logs contains logs of many dates and time.
Sed comes here as a life saver. The below sed command can be used to check logs of certain dates. For example to check all the logs for October 4 do the following.
# sed -n '/^Oct 4/p' /var/log/messages
Also see article on Linux find file command
Cleaning config files
You already know the config files are full of comments.
For any Linux administrator to make your config files more comprehensive, the best idea would be making a copy of the original config files and removing all the commented lines from the config file.
Below is the insight of 'rsyslog.conf' file.
# cat rsyslog.conf
# Log all kernel messages to the console. # Logging much else clutters up the screen. #kern.* /dev/console # Log anything (except mail) of level info or higher. # Don't log private authentication messages! *.info;local0.none;local1.none;mail.none;auth.none;authpriv.none;cron.none /var/log/messages # The authpriv file has restricted access. authpriv.* /var/log/secure # Log all the mail messages in one place. mail.* -/var/log/maillog
Now run the below command to remove all the commented lines from the file and also create a backup of the original file.
# sed -i.$(date +%F) '/^#/d;/^$/d' rsyslog.conf
List your current directory to be sure that the backup has been created.
# ls rsyslog.conf rsyslog.conf.2016-10-11
See how 'rsyslog.conf' file looks after comented lines removed from the file.
# cat rsyslog.conf
$OmitLocalLogging on $IMJournalStateFile imjournal.state *.info;local0.none;local1.none;mail.none;auth.none;authpriv.none;cron.none /var/log/messages authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /var/log/cron *.emerg :omusrmsg:* uucp,news.crit /var/log/spooler local7.* /var/log/boot.log local0.* -/var/log/zimbra.log local1.* -/var/log/zimbra-stats.log
This is it with the tutorial Sed tutorial for Linux administrator (Daily stream editing task)