Sed Command Examples

Managing Users and Groups within Linux

What is SED?


SED is a stream editor that allows you to modify text of an input stream either from a file or output from a command. By default sed should be available on most Linux distributions. To verify this, you can issue the command to display your current version of sed:




john@sles01:~/testing> sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.

The following examples are intended as a brief introduction to SED.


SED Examples


The following test.txt file has been used in the examples below:



john@sles01:~/testing> cat test.txt
one      two       three    four      five          six
John     246810    team01   UK        Birmingham    FTE
Paul     135790    team02   UK        Glasgow       FTE
Marcus   049583    team03   DE        Bremen        PTE
Foxy     903485    team01   UK        Aston         PTE

Simple String Substitution with sed


The following command will search through a file and substitute string1 for string2 and then create a new file called newfile.txt.



john@sles01:~/testing> sed 's/UK/GB/' test.txt > newfile.txt
john@sles01:~/testing> cat newfile.txt
one      two       three    four      five          six
John     246810    team01   GB        Birmingham    FTE
Paul     135790    team02   GB        Glasgow       FTE
Marcus   049583    team03   DE        Bremen        PTE
Foxy     903485    team01   GB        Aston         PTE

In the above example we substituted the string "UK" with the new string "GB".


Extract first line of a file



john@sles01:~/testing> sed -n '1p' test.txt
one      two       three    four      five          six

Extract second line of a file



john@sles01:~/testing> sed -n '2p' test.txt
John     246810    team01   UK        Birmingham    FTE

Extract last line of a file



john@sles01:~/testing> sed -n '$p' test.txt
Foxy     903485    team01   UK        Aston         PTE

Extract all lines of a file except first line



john@sles01:~/testing> sed -n '1!p' test.txt
John     246810    team01   UK        Birmingham    FTE
Paul     135790    team02   UK        Glasgow       FTE
Marcus   049583    team03   DE        Bremen        PTE
Foxy     903485    team01   UK        Aston         PTE

Extract Lines 2,3 and 4 from a file



john@sles01:~/testing> sed -n '2,4p' test.txt
John     246810    team01   UK        Birmingham    FTE
Paul     135790    team02   UK        Glasgow       FTE
Marcus   049583    team03   DE        Bremen        PTE

Extract Lines containing a specified string


The following example displays any line that contains the string "FTE"



john@sles01:~/testing> sed -n -e '/FTE/p' test.txt
John     246810    team01   UK        Birmingham    FTE
Paul     135790    team02   UK        Glasgow       FTE

Extract Lines that start with a specified string


The following example extracts lines from a file that start with the string "John".



john@sles01:~/testing> sed -n -e '/^John/p' test.txt
John     246810    team01   UK        Birmingham    FTE

Extract Lines ending with a specified string


The following example extracts lines that end with the specified string "PTE".


john@sles01:~/testing> sed -n -e '/PTE$/p' test.txt
Marcus   049583    team03   DE        Bremen        PTE
Foxy     903485    team01   UK        Aston         PTE


Remove Blank Lines from a file


In this example, we ill remove any lines that are blank. We can also create a new file from this output by using redirection.
Example file containing blank lines:



john@sles01:~/testing> cat test2.txt
one      two       three    four      five          six

John     246810    team01   UK        Birmingham    FTE

Paul     135790    team02   UK        Glasgow       FTE

Marcus   049583    team03   DE        Bremen        PTE

Foxy     903485    team01   UK        Aston         PTE

issue the command: sed -e '/^$/d' test2.txt



john@sles01:~/testing> sed -e '/^$/d' test2.txt
one      two       three    four      five          six
John     246810    team01   UK        Birmingham    FTE
Paul     135790    team02   UK        Glasgow       FTE
Marcus   049583    team03   DE        Bremen        PTE
Foxy     903485    team01   UK        Aston         PTE

Using redirection to create a new file from the extracted output



john@sles01:~/testing> sed -e '/^$/d' test2.txt > test3.txt

john@sles01:~/testing> cat test3.txt
one      two       three    four      five          six
John     246810    team01   UK        Birmingham    FTE
Paul     135790    team02   UK        Glasgow       FTE
Marcus   049583    team03   DE        Bremen        PTE
Foxy     903485    team01   UK        Aston         PTE

Further reading


A useful resource for the streamline editor sed can be found at the following link. Don't forget, you can always consult the man pages too.

Useful Linux books now available: Linux Books

Online SED Manual: SED Manual