Understanding the sed example

I found a solution to extract the password from a Keychain element for Mac OS X. It uses sedto get the password from the command security:

security 2>&1 >/dev/null find-generic-password -ga $USER | \ 
  sed -En '/^password: / s,^password: "(.*)"$,\1,p'

The code is here in the comment from 'sr105'. The part before |is evaluated as password: "secret". I am trying to figure out how a team works sed. Here are a few thoughts:

I understand the flags -En, but what are the commas in this example? The sed docs say that the comma separates the address range, but there are 3 commas.

The first "address" /^password: /has an end s; the docs sonly mention the replace command, for example s/pattern/replacement/. Not so here.

The part ^password: "(.*)"$looks like a regex to highlight secret, but it is not limited.

I can understand the final part where the back link is printed \1, but again, what do the commas do there?

Note that I'm not interested in a simpler alternative to this sed example. This will only be part of a larger bash script that will include some more sed parsing in the .htaccess file, so I really would like to study the syntax, even if it is unclear.

Thank you for your help!

+4
source share
2 answers

Here is the sed command:

sed -En '/^password: / s,^password: "(.*)"$,\1,p'
  • Commas are used as a regular expression delimiter; it could very well be another delimiter like #:

    sed -En '/^password: / s#^password: "(.*)"$#\1#p'`
    
  • /^password: / finds an input line starting with password:

  • s#^password: "(.*)"$#\1#p password: \1 ( , - )
+5

( ) stdout.

"" sed , sed , , .

/^password: /

, , :

s,^password: "(.*)"$,\1,p

, password: "secret". .

substitute , sed, /. ,.

0

Source: https://habr.com/ru/post/1524582/


All Articles