You are on page 1of 11

The Geek Stuff Home About Free eBook Archives Best of the Blog Contact 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-goi ng 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 The `s command is probably the most important in `sed ptions. s .

and has a lot of different o

The `s command attempts to match the pattern space against the supplied REGEXP; i f the match is successful, then that portion of the pattern space which was matc hed 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 fir st 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 glob al 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 occu rance of Linux to Linux-Unix and prints the substituted output as well as writte n 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. 2. 3. 4. 5. Linux Sysadmin, Linux Scripting etc. Databases - Oracle, mySQL etc. Security (Firewall, Network, Online Security etc) Storage in Linux 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 htm l tags and replaces with the empty. $ sed -e 's/<[^>]*>//g' This <b> is </b> an <i>example</i>. This is an example.

Share Comment If you enjoyed this article, you might also like.. 50 Linux Sysadmin Tutorials 50 Most Frequently Used Linux Commands (With Examples) Top 25 Best Linux Performance Monitoring and Debugging Tools Mommy, I found it! 15 Practical Linux Find Command Examples Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Awk Introduction 7 Awk Print Examples Advanced Sed Substitution Examples 8 Essential Vim Editor Navigation Fundamentals 25 Most Frequently Used Linux IPTables Rules Examples Turbocharge PuTTY with 12 Powerful Add-Ons Bash 101 Hacks Book Vim 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book

Tags: Linux Sed Command, Sed Examples, Sed Tips and Tricks, Unix Sed Command, Un ix Sed Substitution { 31 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 an y luck with making Printer Friendly versions of these pages? I would love to be ab le 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, wh ich 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 art icles. 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 w ork: sed -n /^$/!{s/]*>//g;p;} It s from this site: http://www.unix.com/linux/45584-how-remove-only-html-tags-inside-file.html 11 Srinivas December 27, 2010 at 1:35 pm Excellent! Followed all articles, all are well presented. 12 Piyush January 18, 2011 at 3:01 am Hi, Can i use two or more find and replace at a time using sed. 13 rishi January 25, 2011 at 1:45 am Hi, I am trying to write a script. In /var/log/messages i have to delete lines older than 5days when compared t o the present date. Could you people help me out in this script. Thanks 14 Mui June 26, 2011 at 7:31 pm Hi, After i type in the command, eg sed s/Linux/Linux-Unix/ thegeekstuff.txt ,

I type vi thegeekstuff.txt , it appears that the old file remains. Thus, do i still need to type in anything to save the changes in thegeekstuff.txt ?? Thanks in advance. 15 Brad October 29, 2011 at 3:15 am @Mui, You need to direct the output to another file; otherwise it is written to st andard output, i.e. the terminal screen. Hence: sed s/Linux/Linux-Unix/ thegeekstuff.txt > new_file.txt

Also want to thank the blogger for this site and its edifying content. 16 Ivan Neva February 8, 2012 at 9:28 am Hi, This is a very useful tutorial. I m actually facing a problem with the pattern search, and haven t been able to figure it out. I have a file I need to load to a database, and for that I need to insert a special character when I find the fol lowing sequence of characters: double quotes end of line double quotes ( \n ). I nee d to insert a @ symbol between the first double quotes and the end-of-line ( @\n ). The \n is of course the end-of-line character. So far I m only been able to insert the character using: ( $), sed s/\ $/&@/g filename, but sometimes I need to make sure the next line starts with the double quotes character ( ). Is there any way to do it with SED pattern matching? I have tried with different combinations, but wit

hout any luck. Thanks in advance, great blog by the way. Ivan Neva Oracle DBA 17 Joshua Hurst February 11, 2012 at 12:34 pm Ivan, sed works line by line. There is no sed option that I know of to do what you are looking for. 18 chinna February 20, 2012 at 3:11 pm chinna, i have file like raja ravi kiran i want to add space at the beginning of every line.help me 19 chinna February 22, 2012 at 8:41 am chinna, example 10 i have file like this try this using sed $ sed -e s/\([]\)//g oupput is linux redhat ubuntu thank you 20 chinna February 22, 2012 at 8:45 am chinna, example 10 i have file like this try this using sed $ sed -e s/\([]\)//g oupput is linux redhat ubuntu ******************************************** thank you 21 Mark March 22, 2012 at 6:38 am Hello. I saw how I can replace a word s first occurence in each line. But my q uestion is: how can I replace a word only if it is the very first one in a line? for example: I want to replace pine with apple , and I have the following text: pine cat pine car tag can cat pine tag car here, the word pine from the second line should remain unchanged, as well as t he second occurence in the first line. The result would be:

apple cat pine car tag can cat pine tag car 22 Anonymous March 28, 2012 at 7:21 am mark using the $ sign means at the beginning of a line, so sed s/$pine/apple/ should do it 23 Anonymous March 28, 2012 at 2:16 pm Anonymous, the dollar sign means the end of a line and the carret means the beginning. 24 Priya April 13, 2012 at 11:01 pm sed s/^pine/apple/ filename 25 Senthil April 24, 2012 at 9:46 am Hi, I have an xml file with some properties and I want to replace a value based on the value from other tag. I tried with perl and its working. But I want to do it with sed . please help. example xml file: nameA valueA nameB valueB nameC valueC In the above example, I want to change the value of valueB to user input dat a based on nameB. The hint is nameB is default and will not change. But the valueB can be different even before user input. So, the sed command should find nameB pr operty and then modify tag for it. 26 Mahesh May 30, 2012 at 12:24 am I have file like this and there are many occurances of same parameters. 8109 # CHI { DELAY_TIME 1; } Now i want to change that 1 to 15, but i just cannot find for DELAY_TIME and replace because DELAY_TIME occurs many times. Is there a way i can get this don e? 27 Parthasarathi Dash June 14, 2012 at 3:05 am Try with this command.this is very simple :%s/search_string/replacement_string/g 28 Erik H. June 26, 2012 at 3:45 pm Hi I have this txt-file, containing; 1418W CROMAX ADJUSTER;;588,01 1420W CROLIDT TONEFARVE;;841 1421W CIPAX WHATEVER TONESYSTEM;;462,57

I need to strip it a bit. How do i remove anything between (and includinng) the first e first ; So i m left with a file like; 1418W;;588,01 1420W;;841 1421W;;462,57 29 Prem July 10, 2012 at 7:25 am Hi, Can anyone pls let me know your idea for this scenario: Ex: <![CDUMP welcome to unix world $sorry# line 12 ! Whatever we have is correct Req: I want to remove newline characters in between and Thanks in advance, Prem. 30 Prem July 10, 2012 at 7:26 am Sorry i missed to specify at end of line. Ex: <![CDUMP welcome to unix world $sorry# line 12 ! Whatever we have is correct 31 Monty Gaither August 3, 2012 at 10:18 am

space and until th

I need to change BART that is in every .sh module in a directory to CART. Wo uld the following work? $ sed -g -p Leave a Comment Name E-mail Website Notify me of followup comments via e-mail Previous post: The Ultimate Wget Download Guide With 15 Awesome Examples Next post: Ruby Hello World Example: How To Write and Execute Ruby Program on Un ix OS Sign up for our free email newsletter RSS Twitter Facebook EBOOKS Bash 101 Hacks Book s/BART/CART/g *.*

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Vim 101 Hacks Book Nagios Core 3 Book POPULAR POSTS 12 Amazing and Essential Linux Books To Enrich Your Brain and Library 50 UNIX / Linux Sysadmin Tutorials 50 Most Frequently Used UNIX / Linux Commands (With Examples) How To Be Productive and Get Things Done Using GTD 30 Things To Do When you are Bored and have a Computer Linux Directory Structure (File System Structure) Explained with Example s Linux Crontab: 15 Awesome Cron Job Examples Get a Grip on the Grep! 15 Practical Grep Command Examples Unix LS Command: 15 Practical Examples 15 Examples To Master Linux Command Line History Top 10 Open Source Bug Tracking System Vi and Vim Macro Tutorial: How To Record and Play Mommy, I found it! -- 15 Practical Linux Find Command Examples 15 Awesome Gmail Tips and Tricks 15 Awesome Google Search Tips and Tricks RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams Can You Top This? 15 Practical Linux Top Command Examples Top 5 Best System Monitoring Tools Top 5 Best Linux OS Distributions How To Monitor Remote Linux Host using Nagios 3.0 Awk Introduction Tutorial 7 Awk Print Examples How to Backup Linux? 15 rsync Command Examples The Ultimate Wget Download Guide With 15 Awesome Examples Top 5 Best Linux Text Editors Packet Analyzer: 15 TCPDUMP Command Examples The Ultimate Bash Array Tutorial with 15 Examples 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-cop y-id Unix Sed Tutorial: Advanced Sed Substitution Examples UNIX / Linux: 10 Netstat Command Examples The Ultimate Guide for Creating Strong Passwords 6 Steps to Secure Your Home Wireless Network Turbocharge PuTTY with 12 Powerful Add-Ons About The Geek Stuff Linux 101 Hacks Book My name is Ramesh Natarajan. I will be posting instruct ion guides, how-to, troubleshooting tips and tricks on Linux, database, hardware , security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog. Support Us Support this blog by purchasing one of my ebooks. Bash 101 Hacks eBook Sed and Awk 101 Hacks eBook

Vim 101 Hacks eBook Nagios Core 3 eBook Contact Us Email Me : Use this Contact Form to get in touch me with your comments, ques tions or suggestions about this site. You can also simply drop me a line to say hello!. Follow us on Twitter Become a fan on Facebook Copyright 2008 2012 Ramesh Natarajan. All rights reserved | Terms of Service | Adv ertise

You might also like