How to Search and Replace in Bash easily using sed

Sed in Unix utility is also called stream editor.

It is popularly used in bash scripts. The purpose is to get matching string patterns, and you can replace them with new strings.

The Sed utility you can use at the command line and in scripts (because of automation, you can find usage in all projects).

So I have explained these concepts with a script (match.sh).

After executing the script, you can see it prints both matching rows, and in-between rows of two matching rows.

Bash script of matching string pattern

Bash script of matching string pattern

Here, first condition is /33/, and second condition is both /33/ and /today/.

The -n prints only matching rows. If you omit, it prints both match and non-matching data.

Here is result

Here is result of Bash script

The result you got here is matching rows. The first set is for /33/.

The second set is for matching rows of both the conditions (/33/, /today/). And rows in-between of two matching rows.

Search and Replace

The following examples illustrate how to convert lower case abc to upper case ABC in sed:

echo "abc" |sed "s/abc/ABC/"

The output of the preceding command is here (which only works on one case of abc):

ABC
echo "abcdefabc" |sed "s/abc/ABC/g"

The output of the preceding command is here (/g”) means works on every case of abc):

ABCdefABC

The following sed expression performs three consecutive substitutions, using -e to string them together. It changes exactly one (the first) a to A, one b to B, one c to C:

echo "abcde" |sed -e "s/a/A/" -e "s/b/B/" -e "s/c/C/"

The output of the preceding command is here:

ABCde

Obviously, you can use the following sed expression that combines the three substitutions into one substitution:

echo "abcde" |sed "s/abc/ABC/"

Nevertheless, the –e switch is useful when you need to perform more complex substitutions that cannot be combined into a single substitution.

The “/” character is not the only delimiter that sed supports, which is useful when strings contain the “/” character. For example, you can reverse the order of /aa/bb/cc/ with this command:

echo "/aa/bb/cc" |sed -n "s#/aa/bb/cc#/cc/bb/aa/#p"

The output of the preceding sed command is here:

/cc/bb/aa/

The following examples illustrate how to use the “w” terminal command instruction to write the sed output to both standard output and also to a named file upper1 if the match succeeds:

echo "abcdefabc" |sed "s/abc/ABC/wupper1" ABCdefabc

If you examine the contents of the text file upper1 you will see that it contains the same string ABCdefabc that is displayed on the screen.

This two-stream behavior that we noticed earlier with the print (“p”) terminal command is unusual but sometimes useful. It is more common to simply send the standard output to a file using the “>” syntax, as shown below (both syntaxes work for a replacement operation), but in that case, nothing is written to the terminal screen. The above syntax allows both at the same time:

echo "abcdefabc" | sed "s/abc/ABC/" > upper1
echo "abcdefabc" | sed -n "s/abc/ABC/p" > upper1

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.