Linux Administrator

How to:- Capture Groups using Sed command

Capture groups using Sed command. Manipulating text files plays an important role in the daily life of computer users because word processing is a common activity for users of all professions. Linux and its distributions provide many command line tools and utilities to access and manage text files such as default editor, vim, nano. These tools help to edit, delete, replace text inside a text file; however, the user must open the file with one of these editors and make the necessary changes manually.

Follow this below link to earn more about sed command.

There is another famous editor called “Stream Editor (sed)”; Ubuntu’s sed command line utility provides extensive support for handling text files; This tool is ranked among the best editors because of the advanced features it provides when dealing with text files. The reason for its popularity is single-line command manipulation: that is, it can process text files using the terminal and users don’t have to manually open and edit text files. labour. Capture group mentions another advanced feature of this tool; sed’s group capture feature allows the user to grab a specific portion of a text file or a line. In this in-depth guide, we briefly covered the concept of a capture group, how it works, and how to use it with sed.

Capture Groups using Sed command

In this tutorial we will learn how to capture groups  using sed command.
As stated above, capture groups are a specific part of any line or text file. There may be one of the following purposes behind capture groups:

– To capture information
– Manipulate text for a specific match

It can be used to get the correct information by searching for a specific part in a text file, and manipulation operations can also be performed on that particular match.

How to capture groups using sed command in Linux

Capturing groups in sed are formed by applying parentheses to regular expressions or whatever operation the user wants to perform. For example, to create capture groups, you would put single quotes like “\(” at the beginning and “\)” at the end of a particular regular expression:
In short, capture group is used to get specific part of line, from text file, then perform operation on that group: The following examples demonstrate the use of capture groups using the sed command; Basic to advanced examples.

Capture single group using sed command

To capture single group you can use below command, in below example I am capturing the word “Hello” and also replace the the world after capturing using sed command with “Looklinux“. You can see captured group is enclosed in parenthesis expression “\(” and “\)“.

$ echo Hello sed! | sed 's/\(Hello\) sed!/\1 Looklinux/'
Hello Looklinux

Capture Multiple groups using sed command

The sed command allows you to capture multiple groups and then perform operations on that group. For example, the command mentioned below will capture and print only selected groups.

As you can see in output only three groups printed in reverse order while “Fedora” stays in the original position.

$ echo Ubuntu Debian Linux Fedora | sed 's/\(Ubuntu\) \(Debian\) \(Linux\)/\3 \2 \1/'

Capture complex expressions groups

Suppose we have an expression containing alphanumeric keywords; we need to create groups and then print them in any order (reversed/normal). The command given below shows that the expression contains alphanumeric keywords; we grouped three alphanumeric words together, then displayed the words in reverse order:

Note : We can use the same command by replacing “\w\w*” with “[[:alnum:]_]\{1,\}”:

$ echo Looklinux 123 capture_groups | sed 's/\(\w\w*\) \(\w\w*\) \(\w\w*\)/\3 \2 \1/'

The above command contains capture groups “\(\w\w*\)“; these work for alphanumeric keywords. You can run above command using alphanumeric character class as capture group. For example, the command mentioned below will give the same result when alphanumeric character class is used as capture group.

Conclusion

The Sed command line utility provides detailed instructions for processing text files using the command line terminal; This editor can be difficult to use, but when you dive into the details, you will find it easy to understand and apply. Furthermore, its advanced features ease the process of manipulating and managing text files; like regular expressions and group capture. In this article, we have precisely defined the concept of capturing groups in sed; and provide in-depth usage by referring to some examples. Capture groups are useful, especially when you have very large text files and want to identify specific content from those files.

FAQs

How to capture group using sed command?

To capture groups using sed command execute this command : $ echo Hello sed! | sed ‘s/\(Hello\) sed!/\1 Looklinux/’.

How can I find groups using terminal?

To find the groups in Linux you can use cat command on the file “/etc/group” . When executing this command it will list all available groups on your Linux system.

How to create a group in CentOS/RHEL and Fedora?

You can use groupadd command. To add members to an additional group, use the usermod command to list the additional groups the user is currently a member of and the additional groups the user will become a member of.

How to create a group in Ubuntu?

Use this command to add the group in Ubuntu or Debian base system : $ sudo addgroup group_name

How to change group of a folder in Linux?

Changing directory permission for group owner is the similar command to change the group of a folder in Linux, but add a “g” for group and “o” for users.

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment