Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx

by Sasikala on September 30, 2009

Sed Examples for Unix and Linux - Find and ReplaceThis article is part of on-going Unix Sed Tutorial series. In previous articles, we discussed about sed print operation and sed delete operation.

In this article let us review how to use sed substitute command “s”.

The `s’ command is probably the most important in `sed’ and has a lot of different options.

The `s’ command attempts to match the pattern space against the supplied REGEXP; if the match is successful, then that portion of the pattern space which was matched is replaced with REPLACEMENT.

Syntax:

#sed 'ADDRESSs/REGEXP/REPLACEMENT/FLAGS' filename
#sed 'PATTERNs/REGEXP/REPLACEMENT/FLAGS' filename
  • s is substitute command
  • / is a delimiter
  • REGEXP is regular expression to match
  • REPLACEMENT is a value to replace

FLAGS can be any of the following

  • g Replace all the instance of REGEXP with REPLACEMENT
  • n Could be any number,replace nth instance of the REGEXP with REPLACEMENT.
  • p If substitution was made, then prints the new pattern space.
  • i match REGEXP in a case-insensitive manner.
  • w file If substitution was made, write out the result to the given file.
  • We can use different delimiters ( one of @ % ; : ) instead of /

Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

$ cat thegeekstuff.txt
# Instruction Guides
1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not much time available)
#  Additional FAQS
6. Windows- Sysadmin, reboot etc.

Let us review some interesting examples for substitution now.

1. Substitute Word “Linux” to “Linux-Unix” Using sed s//

In the example below, in the output line “1. Linux-Unix Sysadmin, Linux Scripting etc” only first Linux is replaced by Linux-Unix. If no flags are specified the first match of line is replaced.

$ sed 's/Linux/Linux-Unix/' thegeekstuff.txt
# Instruction Guides
1. Linux-Unix Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux-Unix
5. Productivity (Too many technologies to explore, not much time available)
#  Additional FAQS
6. Windows- Sysadmin, reboot etc.

2. Substitute all Appearances of a Word Using sed s//g

The below sed command replaces all occurrences of Linux to Linux-Unix using global substitution flag “g”.

$ sed 's/Linux/Linux-Unix/g' thegeekstuff.txt
# Instruction Guides
1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux-Unix
5. Productivity (Too many technologies to explore, not much time available)
#  Additional FAQS
6. Windows- Sysadmin, reboot etc.

3. Substitute Only 2nd Occurrence of a Word Using sed s//2

In the example below, in the output line “1. Linux Sysadmin, Linux-Unix Scripting etc.” only 2nd occurance of Linux is replaced by Linux-Unix.

$ sed 's/Linux/Linux-Unix/2' thegeekstuff.txt
# Instruction Guides
1. Linux Sysadmin, Linux-Unix Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not much time available)
#  Additional FAQS
6. Windows- Sysadmin, reboot etc.

4. Write Changes to a File and Print the Changes Using sed s//gpw

The example below has substitution with three flags. It substitutes all the occurance of Linux to Linux-Unix and prints the substituted output as well as written the same to the given the file.

$ sed -n 's/Linux/Linux-Unix/gpw output' thegeekstuff.txt
1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
4. Storage in Linux-Unix
$ cat output
1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
4. Storage in Linux-Unix

5. Substitute Only When the Line Matches with the Pattern Using sed

In this example, if the line matches with the pattern “-”, then it replaces all the characters from “-” with the empty.

$ sed '/\-/s/\-.*//g' thegeekstuff.txt
# Instruction Guides
1. Linux Sysadmin, Linux Scripting etc.
2. Databases
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not much time available)
#  Additional FAQS
6. Windows

6. Delete Last X Number of Characters From Each Line Using sed

This sed example deletes last 3 characters from each line.

$ sed 's/...$//' thegeekstuff.txt
# Instruction Gui
1. Linux Sysadmin, Linux Scripting e
2. Databases - Oracle, mySQL e
3. Security (Firewall, Network, Online Security e
4. Storage in Li
5. Productivity (Too many technologies to explore, not much time availab
#  Additional F
6. Windows- Sysadmin, reboot e

7. Eliminate Comments Using sed

Delete all the comment lines from a file as shown below using sed command.

$  sed -e 's/#.*//' thegeekstuff.txt

1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not much time available)

6. Windows- Sysadmin, reboot etc.

8. Eliminate Comments and Empty Lines Using sed

In this example, there are two commands seperated by ‘;’

  • First command replaces the lines starting with the # to the blank lines
  • Second command deletes the empty lines.
$ sed -e 's/#.*//;/^$/d'  thegeekstuff.txt
1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not much time available)
6. Windows- Sysadmin, reboot etc.

9. Convert DOS newlines (CR/LF) to Unix format Using sed

Copy the DOS file to Unix, you could find \r\n in the end of each line.

This example converts the DOS file format to Unix file format using sed command.

$sed 's/.$//' filename

10. Eliminate HTML Tags from file Using sed

In this example, the regular expression given in the sed command matches the html tags and replaces with the empty.

$ sed -e 's/<[^>]*>//g'
This <b> is </b> an <i>example</i>.
This  is  an example.
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Unix Sed Tutorial: Append, Insert, Replace, and Count File Lines
  2. Unix Sed Tutorial: Delete File Lines Using Address and Patterns
  3. Unix Sed Tutorial: How To Write to a File Using Sed
  4. Unix Sed Tutorial: Printing File Lines using Address and Patterns
  5. Unix Sed Tutorial: How To Execute Multiple Sed Commands
  

Vim 101 Hacks Book

{ 2 trackbacks }

Danaville » Blog Archive » unix
September 30, 2009 at 10:52 pm
Destillat KW40-2009 | duetsch.info - GNU/Linux, Open Source, Softwareentwicklung, Selbstmanagement, Vim ...
October 2, 2009 at 4:54 am

{ 10 comments… read them below or add one }

1 Marcus Rhodes September 30, 2009 at 7:47 am

How would one rearrange the columns output by ls -al?

On AIX, which lacks gnu/Linux’s excellent features, I need ls -al to output …

The filename -rwxrwxrwx 1 root root 12345678 …

… instead of …

-rwxrwxrwx 1 root root 12345678 Jan 01 2009 The filename

Thanks!

2 Paul M September 30, 2009 at 11:15 am

Ramesh,

I am getting so much benefit from these articles. Thank you! Have you had any luck with making “Printer Friendly” versions of these pages? I would love to be able to print them out and write down my own notes on them.

Paul

3 Jimi September 30, 2009 at 1:25 pm

Hi,

there seems to be some typo in example 10. I am getting this output line, which obviously is not what you intented:

This <b is </b an <iexample</i

I could not come up with the correct version myself. Can you help?

4 Sasikala September 30, 2009 at 11:04 pm

@Marcus Rhodes,

Hope this helps,
$ ls -al The\ Geek\ Stuff | sed -e ’s/\(\([^ ]* \+\)\{8\}\)\(.*\)$/\3 \1/g’
The Geek Stuff -rw-r–r– 1 user group 0 Sep 30 21:50

@Jimi,

Thanks for catching the typo.
Example: 10 command should be
$ sed -e ’s/<[^>]*>//g’
Its for removing simple html tags.

5 amksep October 1, 2009 at 1:38 am

for example 10 i think the right syntax is
sed -e ’s/]*>//g’

6 amksep October 1, 2009 at 1:39 am

sorry you are right Jimi.

7 Nasrin October 1, 2009 at 8:58 am

@ Sasikala

$ sed -e ’s/]*>//g’
bash: [^: No such file or directory

this also is not working :(

8 Ramesh Natarajan October 2, 2009 at 10:19 am

@Paul,

Print Friendly option is implemented. You’ll see this at the bottom of all articles. Thanks.

9 Marcus Rhodes October 2, 2009 at 1:31 pm

Hmmm… works on Ubuntu, though the filenames need padding, but has no effect on AIX.

10 Jimi October 8, 2009 at 1:48 pm

I did some googling on Example 10, and found this solution, which seems to work:

sed -n ‘/^$/!{s/]*>//g;p;}’

It’s from this site:

http://www.unix.com/linux/45584-how-remove-only-html-tags-inside-file.html

Leave a Comment

Previous post:

Next post: