News for hackers. Stuff that matters.
Re:Useful one-line scripts for sed (2005) by majewsky on Thursday June 08, 2017 @04:47AM (#14512842)
I use stuff like that all the time with config files where sections span multiple lines. For example, if ~/.ssh/config has a section like

  Host example
    HostName example.com
    IdentityFile ~/.ssh/id_example
    Port 1234

  Host next
    ...
Then the following selects the identity file:

  sed -n '/^Host example/,/^Host\>/{/^\s*IdentityFile/{s/^\s*IdentityFile\s*//;p}}' ~/.ssh/config
Although I would probably use awk after sed because it handled space-separated fields nicer:

  sed -n '/^Host example/,/^Host\>/p' ~/.ssh/config | awk '$1=="IdentityFile"{print$2}'

There are no comments on this comment yet.