Find and Replace Text Inside A File Using RegEx
Find and Replace Text Inside A File Using RegEx
Find and Replace Text Inside A File Using RegEx
1 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
Home
About
Free eBook
Archives
Best of the Blog
Contact
Free Dating
Ads by Google
Dating Online
Dating Websites
Dating Site
Like
Tweet
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
2 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
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.
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
3 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
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
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
4 of 12
3.
4.
5.
#
6.
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
1.
2.
3.
4.
5.
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
5 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
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.
Tweet
Like
Share
Comment
Tags: Linux Sed Command, Sed Examples, Sed Tips and Tricks, Unix Sed Command, Unix Sed Substitution
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
6 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
7 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
8 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
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 standard 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.
Leave a Comment
Name
E-mail
Website
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 Unix OS
Sign up for our free email newsletter [email protected]
RSS
Sign Up
Search
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
9 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
EBOOKS
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
10 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
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 Examples
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-copy-id
Unix Sed Tutorial: Advanced Sed Substitution Examples
UNIX / Linux: 10 Netstat Command Examples
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
11 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
Support Us
1/12/2012 12:28 AM
Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx
12 of 12
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-insid...
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions 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 20082012 Ramesh Natarajan. All rights reserved | Terms of Service | Advertise
1/12/2012 12:28 AM