How can I output only captured groups with sed?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How can I output only captured groups with sed?

How to Output Only Captured Groups with sed

Have you ever wondered how to extract specific parts of a text using sed? 🤔 Well, you're in the right place! In this guide, we will go through a common issue: how to output only captured groups using sed. We'll provide easy solutions and show you how to do it step by step. Let's dive in! 💪

The Problem

Often, we want to extract specific patterns or groups from a text and discard the rest. In the given context, we have the following input:

This is a sample 123 text and some 987 numbers

And our desired pattern is:

/([\d]+)/

Our objective is to output the captured groups, that is, the numbers 123 and 987, while ignoring the rest of the text.

The Solution

To achieve this, we can use the power of regular expressions and the functionality provided by sed.

The key here is to use capture groups, which are defined within parentheses ( ) in our pattern. Whatever matches the content inside these groups will be stored separately. To output only the captured groups, we need to refer to them using back-references.

Here's how you can do it step by step with sed:

  1. Open your terminal and execute the following command:

sed -n 's/.*\([\d]\+\).*/\1/p'
  1. Let's break down the command:

  • -n tells sed not to print lines by default.

  • The command s/.*\([\d]\+\).*/\1/ performs a substitution using the pattern and the replacement.

    • .* matches any character zero or more times, allowing us to discard the initial part of the line.

    • \([\d]\+\) captures the desired group of digits. We used \+ to indicate one or more occurrences of digits.

    • .* matches any character zero or more times, allowing us to discard the remaining part of the line.

    • \1 refers to the first captured group via back-reference, which is the only thing printed.

  1. Execute the command, providing the input text:

This is a sample 123 text and some 987 numbers
  1. The output will be:

123
987

And voilà! 🎉 Using sed and our regular expression pattern, we were able to extract only the desired captured groups from the input text. Pretty cool, right? 😉

Conclusion

Extracting specific parts of text using sed doesn't have to be complicated. By using capture groups and back-references, we can easily output only the content we want while ignoring the rest. 💬

Now it's your turn! Give it a try with your own input and patterns. Play around with different regular expressions and see what you can achieve. We'd love to hear about your experiences and any other tips or tricks you might have. 😄

Leave a comment below and let us know how it went! Happy seding! ✨✍️

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my