Sed replace command in unix and linux

sed replace command
Sed replace command

Sed replace introduction

Most people use sed replace command mainly for replacing strings and variables in a file. However, by combining different search patterns with sed you can find and replace streams which can be a little gruelling when doing it with other tools.


Sed replace in file

You can do the global in-place substitution on the file with the following command. The option 's/' is used here to substitute a pattern in a file. 'g' is used here for global replacement. 'i' is used for in-place editing in file so that the changes you make will be permanent in file.

sed -i 's/find_pattern /replace_pattern /g' filename


Sed replace string in file

You can replace variable and strings in a file with sed. Let's replace string 'foo' with 'bar' in all the files present in the current directory.

Go to the directory where you want to run the below command.

sed -i 's/foo/bar/g' *

This replaces recursively all files containing string 'foo' with 'bar' in the current directory.

or you can replace the pattern by defining the directory path.

sed -i 's/foo/bar/g' test/*

Here, test is a directory.

You can use sed to replace files which contains specific extensions. like '.txt'

sed -i 's/foo/bar/g' test/*.txt

If sed couldn't find any such files with the given extension it throws the below error.

sed: can't read test/*.txt: No such file or directory


Sed replace with variable

With sed You can replace the value of the variable by directly accessing the variable. The below example will clarify the statement more clearly.

Let's say you have a file called 'script' with the following content in it.

var= 034

You want to change the value of the variable without directly editing or replacing the value.

sed '/^var/s/^\(.*=\s*\).*$/\1219/' script

var= 219

The above command replaces the value of the variable without directly editing the value.

Note: We have used back-reference here '\1' . Back-references are regular expression command. It can be used in sed replacement 's' and regular expression search pattern.

Sed tutorial for linux administrator

Sed replace string with variable

Similarly, you can replace any string by accessing it's variable.

We have a file which contains string variable.

cat script

str="how are you"

Let's replace the string.

sed '/^str/s/^\(.*=\s*\).*$/\1fine/' script



Sed replace regex

You can use regex with sed to replace complex string .

Suppose you have a file in your system which contains lots of commented lines. Now you want to remove those comments from file.

cat script
# Let's use sed replace
sed 's/^# //' script

Let's use sed replace

The above sed replace command will search for lines which contains comment (#) at the beginning and replace it.


 Sed command in unix and linux

Sed replace new line

Well you can turn a new line with spaces. Consider you are having a text in your file which are arranged indefinitely. Instead of new lines between words you want spaces between them.

cat file


You can replace the new lines above into spaces.

sed ':e;N;$!be;s/\n/ /g' file

Hi how are you


Sed replace first occurrence in file

By default sed replaces only the first occurrences in each line.

cat file

please go
please go
sed 's/go/come/' file

please come
please come

You can instruct sed to replace only the first matched occurrences in the complete file.

sed '0,/go/s//come/' file


Sed replace line number

You can replace a text or pattern in a specific line.

cat file

This is line number 1 A
This is line number 2 B
This is line number 3 C
This is line number 4 D

For example let's replace a text at line number 4.

sed '4s/D/E/' file

This is line number 1 A
This is line number 2 B
This is line number 3 C
This is line number 4 E

You can even replace a complete whole line with a new line.

sed '4s/.*/You have replaced the line successfully/' file

This is line number 1 A
This is line number 2 B
This is line number 3 C
You have replaced the line successfully


Sed replace multiple spaces with comma

Some times you find extra spaces or spaces which are not required at all between words. you may replace them with comma.

This is a file where every word has 3 spaces gaph between them. Let's replace the spaces with comma.

cat file

Computer   mouse   keyboard   switch   router
sed -r 's/ {3}/,/g' file


In case if there are 4 spaces gaph between words. You have to use {4} in your command.

Download Our Free eBook now

Linux and UNIX Shell scripting ebook

Be the first to comment

Leave a Reply

Your email address will not be published.