0

I'm trying to learn to use grep groups, like sed \1\2\3, but have a problem. For example I filtering /etc/services file to separate all ports. What I do:

~$ grep -E '[0-9]{1,5}/(tcp|udp)' /etc/services

and now I get 'port/protocol'. Next, I try separate it with groups:

~$ grep -E '\([0-9]{1,5}\)/(tcp|udp)' /etc/services

and haven't any effect. Well, trying non extended grep:

~$ grep '\([0-9]*\)/[tcp\|udp]\1' /etc/services

but results not right (/t or /u). So, how to use groups?

1
  • 1
    "...but results not right (/t or /u)" What output do you want, exactly? Commented May 18, 2015 at 11:30

1 Answer 1

1

You are referring to regex back-references.

Please check these two references:

https://stackoverflow.com/questions/4609949/what-does-1-in-sed-do

http://www.gnu.org/software/grep/manual/html_node/Back_002dreferences-and-Subexpressions.html

And see the output of grep '\([0-9]\)\1' /etc/services which will give you a resultset of lines where a digit is directly followed by the same digit (the back reference \1).

1
  • (...) would capture the characters specified inside of the parens and \1 would be used to reference the first match, this is a part of regex. and I think I trying do same
    – conformist
    Commented May 18, 2015 at 9:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .