sed: print only matching group

sed print only matching lines
sed print all matches
grep print only match group
sed regex
awk print only matching pattern
man sed
sed match lines starting with
sed tutorial

I want to grab the last two numbers (one int, one float; followed by optional whitespace) and print only them.

Example:

foo bar <foo> bla 1 2 3.4

Should print:

2 3.4

So far, I have the following:

sed -n  's/\([0-9][0-9]*[\ \t][0-9.]*[\ \t]*$\)/replacement/p' 

will give me

foo bar <foo> bla 1 replacement

However, if I try to replace it with group 1, the whole line is printed.

sed -n  's/\([0-9][0-9]*[\ \t][0-9.]*[\ \t]*$\)/\1/p' 

How can I print only the section of the line that matches the regex in the group?

Match the whole line, so add a .* at the beginning of your regex. This causes the entire line to be replaced with the contents of the group

echo "foo bar <foo> bla 1 2 3.4" |
 sed -n  's/.*\([0-9][0-9]*[\ \t][0-9.]*[ \t]*$\)/\1/p'
2 3.4

How to output only captured groups with sed?, print the substitution ( p ). In general, in sed you capture groups using parentheses and output what you capture using a back reference: echo  With sed, the -n option suppresses printing of the complete file and the p command is used to print the matching pattern. We would like to replace the first entry with a custom entry of our own. We will also add in the prefer option to ensure that this is used for synchronisation as long as it is available. Using this method we can sync with our own local NTP server whilst maintaining the other as failover backups. Matching the First Line Only

grep is the right tool for extracting.

using your example and your regex:

kent$  echo 'foo bar <foo> bla 1 2 3.4'|grep -o '[0-9][0-9]*[\ \t][0-9.]*[\ \t]*$'
2 3.4

Only return the matched string in sed, Closed 7 years ago. Possible Duplicate: Extracting a regex matched with 'sed' without printing the surrounding characters. How do I make this only print test : Lets say we need to print only strings between two lines that contain patterns ‘BEGIN’ and ‘END’. Print Lines Between Two Patterns with SED. With the sed command, we can specify the starting pattern and the ending pattern, to print the lines between strings with these patterns. The syntax and the example are shown below. Syntax:

And for yet another option, I'd go with awk!

echo "foo bar <foo> bla 1 2 3.4" | awk '{ print $(NF-1), $NF; }'

This will split the input (I'm using STDIN here, but your input could easily be a file) on spaces, and then print out the last-but-one field, and then the last field. The $NF variables hold the number of fields found after exploding on spaces.

The benefit of this is that it doesn't matter if what precedes the last two fields changes, as long as you only ever want the last two it'll continue to work.

Sed only print matched expression, Make the regexp a little more specific. sed -n 's/.* \([0-9]*%\),.*/Battery: \1/p'. Pick a different tool. perl -ne '/(\d+%)/ && print "Battery: $1\n";'. sed: print only matching group. Ask Question Asked 6 years, 11 months ago. Active 8 months ago. Viewed 216k times 130. 16. I want to grab the last two numbers (one

The cut command is designed for this exact situation. It will "cut" on any delimiter and then you can specify which chunks should be output.

For instance: echo "foo bar <foo> bla 1 2 3.4" | cut -d " " -f 6-7

Will result in output of: 2 3.4

-d sets the delimiter

-f selects the range of 'fields' to output, in this case, it's the 6th through 7th chunks of the original string. You can also specify the range as a list, such as 6,7.

Sed Command in Linux - Print Lines in a File, of pattern buffer/space. So, we would want to use this option. There are many useful flags such as -E(extended regular expression) or -P(perl like regular expression), -v(–invert, select non-matching lines) or -o(show matched part only). So some day I want to output capture group only. After Googling, many people are actually suggesting sed–sadly I am not good with sed.

I agree with @kent that this is well suited for grep -o. If you need to extract a group within a pattern, you can do it with a 2nd grep.

# To extract \1 from /xx([0-9]+)yy/
$ echo "aa678bb xx123yy xx4yy aa42 aa9bb" | grep -Eo 'xx[0-9]+yy' | grep -Eo '[0-9]+'
123
4

# To extract \1 from /a([0-9]+)b/
$ echo "aa678bb xx123yy xx4yy aa42 aa9bb" | grep -Eo 'a[0-9]+b' | grep -Eo '[0-9]+'
678
9

20 Sed (Stream Editor) Command Examples for Linux Users, or Stream Editor is very powerful utility offered by Linux/Unix systems. It is mainly used for text substitution , find & replace but it can also perform other text manipulations like insertion, deletion, search etc. If you want only what is in the parenthesis, you need something that supports capturing sub matches (Named or Numbered Capturing Groups). I don't think grep or egrep can do this, perl and sed can. For example, with perl: If a file called foo has a line in that is as follows: /adsdds / And you do: perl -nle 'print $1 if /\/(\w).+\//' foo

Can GNU Grep output a selected group?, This doesn't work for multiline input. For that you need: sed -n "s/.*'\([^']\+\)'. While grep can't output a specific group, you can use lookahead and behind assertions to achieve what your after: You can use \K to reset and discard the left hand match text along with a lookahead which is not added to the GNU grep only. Description. sed is a stream editor.A stream editor is used to perform basic text transformations on an input stream (a file, or input from a pipeline).While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.

Sed One-Liners Explained, Part II: Selective Printing of Certain Lines, The "!" before "d" command inverts the line matching. 51. Print only the lines that do not match a regular expression (emulates "grep -v"). sed -n '/regexp/!  In our previous sed articles we learned — sed printing, sed deletion, sed substitute , sed file write, and sed multiple commands. In this article, let us review some interesting workarounds with the “s” substitute command in sed with several practical examples.

[100% Working Code] - sed: print only matching group, sed: print only matching group: Because it match the whole line, so add a .* at the beginning of regex. This causes the entire line to be replaced 

Comments
  • I had to add the -r or ` --regexp-extended` option otherwise I was getting invalid reference \1 on s' command's RHS ` error.
  • @DanielSokolowski I think you get that error if you use ( and ) instead of \( and \).
  • Also remember to add .* to the end of the regexp if the string you want to extract is not always at the end of the line.
  • This won't work for me because .* is greedy and sed doesn't have a non-greedy .*?
  • @DanielDarabos Just mention that ( and ) will not raise error in ubuntu 16.04. So I think this comment is outdated.
  • great for the entire group, though sed is needed for individual groups
  • grep -o does not port on systems running msysgit but sed does.
  • See the question linked by @jozxyqk for an answer that uses look-ahead and look-behind to solve this with grep.
  • You can extract a group from a pattern with piped grep -o calls. stackoverflow.com/a/58314379/117471
  • To print only certain columns, pipe to awk '{ print $2" "$6 }'
  • @nurettin I think your comment might have been meant for one of the awk answers.
  • I tried cut when I visited this page and realized it's limitations and decided to write a more generalized version in awk instead as a comment to improve the quality of this post.
  • Yeah, I think that belongs on a different answer involving awk. The cut command to do what you wrote is: cut -d " " -f 2,6
  • ah, I didn't know that, I thought you could only give ranges. Thanks for that.