Tuesday, 5 January 2016

How To Replace Text Using Sed in Unix/Linux

This article will describe the basics of sed command. sed command is very powerful utility to perform text replacement, file modification, text modification and many more. We usually see sed as replacement command which is only a property of sed out of many. We will see number of posts on sed, here we will cover basic so that subsequent posts/articles will be easy to understand.

Input File: F_Input_Data

$ cat F_Input_Data
A1|TRANS|Y|100000
A2|MED|Y|20000
A3|FIN|N|10000
A4|HR|Y|20000
A5|CSR|N|50000
A6|TRANS|Y|30000
A7|FIN|Y|40000

Basic Syntax: 

sed 's/reg_exp/replacement/[occurrence]' F_Input_File

s- stands for substitution
reg_exp : Pattern to be searched
replacement : Searched pattern to be replaced with
occurrence: which occurrence of rex_exp needs to be replaced.

1. How to replace TRANS with TRANSPORT

$ sed 's/TRANS/TRANSPORT/' F_Input_Data

Output: 
sed will return below output, here regular expression us TRANS and replacement string is  TRANSPORT.  If you observe sed has only replace TRANS to TRANSPORT available in first record. It has not changed 6th record(A6) this is because by default sed replaces first occurrence of regular expression. We can force sed to replace particular occurrence of a regular expression.

A1|TRANSPORT|Y|100000
A2|MED|Y|20000
A3|FIN|N|10000
A4|HR|Y|20000
A5|CSR|N|50000
A6|TRANS|Y|30000
A7|FIN|Y|40000
A8|TRANS|N|35000

Note: Here sed will not change the content of the file it only displays the updated data on terminal. We can also force sed to make change in file itself, how? will see in upcoming examples or posts.

2. How to replace 3rd occurrence of TRANS with TRANSPORT

$ sed 's/TRANS/TRANSPORT/3' F_Input_Data

Output: It will replace only 3rd occurrence of TRANS with TRANSPORT.
A1|TRANS|Y|100000
A2|MED|Y|20000
A3|FIN|N|10000
A4|HR|Y|20000
A5|CSR|N|50000
A6|TRANS|Y|30000
A7|FIN|Y|40000
A8|TRANSPORT|N|35000

3. How to replace TRANS to TRANSPORT in complete file.

$ sed 's/TRANS/TRANSPORT/g' F_Input_Data

Output: It will replace all the occurrence of TRANS with TRANSPORT. If you observe this time we have sed g flag in sed command which stands for global replacement.

A1|TRANS|Y|100000
A2|MED|Y|20000
A3|FIN|N|10000
A4|HR|Y|20000
A5|CSR|N|50000
A6|TRANS|Y|30000
A7|FIN|Y|40000
A8|TRANSPORT|N|35000

4. How to replace a string which has slash(/) in it.

Let we want to replace a path /1/2/3/a/b/c in existing script with /1/2/3/x/y/z

$  echo "/1/2/3/a/b/c" | sed 's/\/a\/b\/c/\/x\/y\/z/g' 

Output:  Will replace /1/2/3/a/b/c to /1/2/3/x/y/z

If we see the the above solution it seems very complex and difficult to understand. Here we have to skip slash(/) by escape character(\). It is not necessary that we have to only use slash(/) as a delimiter in sed. We can use anything we want to make the code more readable.

Below code will also provide the same solution as provided by initial solution

$  echo "/1/2/3/a/b/c" | sed 's|/a/b/c|/x/y/z|g'

[OR]

$  echo "/1/2/3/a/b/c" | sed 's:/a/b/c:/x/y/z:g'

[OR]

$  echo "/1/2/3/a/b/c" | sed 's@/a/b/c@/x/y/z@g'

Output: Will replace /1/2/3/a/b/c to /1/2/3/x/y/z

Now solution looks simpler and easy to understand.

Till now we have seen that the data is not updating in file or changes are not permanent in the file. We can do the changes permanent by redirecting the output of the send command in another file.

$ sed 's/TRANS/TRANSPORT/g' F_Input_Data > F_Output_Data

[OR]

$ sed 's/TRANS/TRANSPORT/g' < F_Input_Data > F_Output_Data

In this case changed/updated data will be available in F_Output_Data.

5. How to handle single quote(') and double quote(") in sed
let say we want to update is as 'is' in Unix is great. Let see how we do that.

$ echo "Unix is great" | sed 's/is/'is'/g'
Output: This will not provide any output and will result in error. To overcome from this we have to use below syntax.

$ echo "Unix is great" | sed "s/is/'is'/g"
Output: Unix 'is' great.


$ echo "Unix is great" | sed 's/is/"is"/g'
Output: Unix "is" great.


Keep Reading, Keep Learning, Keep Sharing...!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...