DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat

Using sed to perform inline replacements of regex groups

Alex Holmes user avatar by
Alex Holmes
·
Feb. 14, 13 · Interview
Like (0)
Save
Tweet
Share
4.08K Views

Join the DZone community and get the full member experience.

Join For Free

I love tools like sed and awk - I use them every day, and only realize how much I rely on them when I’m forced to work on a machine that’s not running Unix. Today I want to look at a feature that is really useful when working with regular expressions in sed.

Imagine that you had an IP address, and you wanted to change the second octet - one way to do this in sed is the following:

shell$ echo "127.0.0.1" | sed "s/127.0/127.1/"
127.1.0.1

That seemed to work well, and was simple. But what if you had a file of random IP’s - how would you change the second octet in that scenario? Sure, you could use awk, but that feels like it would be overkill. Well, it can be done in sed with something called regular expression group substitutions.

First of all, you’ll need to tell sed that you are using extended regular expressions by using the -r option, so that you don’t have to escape some of the regular expression characters (if you’re curious, they are ?+(){}). If you end up needing to use any of these characters as literals, you’ll ned to escape them with a backslash (\).

sed supports up to 9 groups that can be defined in the pattern string, and subsequently referenced in the replacement string. In the following command the pattern string starts with a group, which contains the first octet followed by the period, and that’s followed by a second octet. In the replacement string we’re referencing the first (and only) group with \1, followed by 234 which is the replacement for the rest of the matching string, which contains the second octet.

shell$ echo "127.0.0.1" | sed -r "s/^([0-9]{1,3}\.)[0-9]{1,3}/\1234/"
127.234.0.1

What if we wanted to preserve the second octet and simply a “1” in front of it? In that case you can define a second group in the pattern, and reference the second group in the replacement value:

shell$ echo "127.0.0.1" | sed -r "s/^([0-9]{1,3}\.)([0-9]{1,3})/\11\2/"
127.10.0.1

Actually it would have been easier to just remove the second octet altogether from the pattern:

shell$ echo "127.0.0.1" | sed -r "s/^([0-9]{1,3}\.)/\11/"
127.10.0.1

On a final note - it wasn’t so long ago that I would write a command similar to the one below if I wanted to use sed to perform a substitution and overwrite an existing file:

shell$ sed 's/a/b/' file1.txt > file2.txt; mv file2.txt file1.txt

Ugh! Well there’s no need to do this - sed has a -i option which will do an inline replace of the file:

shell$ sed -i 's/a/b/' file1.txt

Ahhh, that’s better! Anything that’s easy on the eyes gets a thumbs-up from me.

Sed

Published at DZone with permission of Alex Holmes, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Handling Virtual Threads
  • Integration: Data, Security, Challenges, and Best Solutions
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: