-2

I've been told to use this regex with sed:

sed 's/^.*=\([^=]*\)=.*=.*$/\1/' 

to get the PATH part from:

NAME=PATH=USER=DATE

If I "read" the regex i would say something like: From the beginning of the line (^),any character any number of times (.*) and then an equal sign (=). There I would have gotten just NAME=. What I don't understand is what to read next, the \( \) part.

I've seen this multiple times and I know it has something to do with the /\1 at the end of the expression but I don't quite get it.

The other part (=.*=.*$) would be: from an equal sign (=) any character any number of times (.*) until it reads an equal (=) and again, any character until reaching end of line ($).

devnull
  • 103,635
  • 29
  • 207
  • 208
user3013172
  • 1,273
  • 2
  • 11
  • 20
  • Try [regex 101](http://regex101.com/). Or [regexper](http://www.regexper.com/#%5E.*%3D%5C(%5B%5E%3D%5D*%5C)%3D.*%3D.*%24). – Boris the Spider Apr 27 '14 at 19:08
  • [Debuggex Demo](https://www.debuggex.com/r/UEWO-TAJ0jeNdFz5) – h2ooooooo Apr 27 '14 at 19:11
  • @Boris: doesn't work for sed - the `\(` here aren't escaped parens like they are in f.e. perl. – Mat Apr 27 '14 at 19:11
  • Why didn't you ask when you got [this answer](http://stackoverflow.com/a/23324527/2235132)? I don't see a reason to make another post for this? – devnull Apr 27 '14 at 19:30
  • This question appears to be off-topic because it is about clarifying one of the response received in [OP's another question](http://stackoverflow.com/questions/23324406/get-something-between-separators-with-regular-expressions). – devnull Apr 27 '14 at 19:31

5 Answers5

3

The \([^=]*\) is for capturing the matched part inside the parenthesis. You can then use this match in the second part of the s/// command as \1.

Generally, you can use \N to use the Nth captured partial match or & to substitute the complete matched regular expression.

See Overview of Regular Expression Syntax for details of the regular expression used in sed.

Olaf Dietsche
  • 66,104
  • 6
  • 91
  • 177
1

the parentheses group the result you wish to capture, in this case "any number of characters which are not the '=' character". The string matched by the pattern inside the parentheses can be referred to later as \1 -- or \2, \3 etc if there were subsequent groups in parentheses. the '\' before the parentheses are needed to escape the (,) characters so they are interpreted as grouping symbols -- not certain if they need to be escaped this way for sed or from the shell interpreter reading the expression.

robm
  • 1,133
  • 1
  • 12
  • 22
1

To get the PATH i would have used a simple awk like this:

echo "NAME=PATH=USER=DATE" | awk -F= '{print $2}'
PATH

Easy to understand, and work nice.
Device text by = and then print second field.

Jotne
  • 38,154
  • 10
  • 46
  • 52
0

or with cut

$ echo "NAME=PATH=USER=DATE" | cut -d= -f2
PATH
Red Cricket
  • 7,996
  • 14
  • 57
  • 130
-3

The \s before the brackets are for escaping.

It means, "The start of the string (^), any character any number of times (.\*), then an equal sign (=), then an opening bracket ('\('), then any character EXCEPT '=', any number of times ([^=]\*), then a closing bracket ('\)'), then an equal sign (=), then any character any number of times (.\*), then an equal sign (=), then any character any number of times (.\*), then the end of the string ($)."

vinit_ivar
  • 530
  • 3
  • 15