1

I need more help in this case, to do something similar to a grep between two patterns: I need to look for Everyone Allow FullControl

Say I have this:

    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\Service_Legal 
    Access : BUILTIN\Administrators Allow  FullControl
    -----------------------
    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\balco 
    Access : Everyone Allow  268435456
             Everyone Allow  FullControl
    -----------------------
    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\BCDRPlanning 
    Access : Everyone Allow  FullControl
             Everyone Allow  268435456
    -----------------------

so I would get something similar to this:

    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\balco 
             Everyone Allow  FullControl
    -----------------------
    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\BCDRPlanning 
    Access : Everyone Allow  FullControl

I did not know how to do this so I come here

Eduardo
  • 397
  • 2
  • 6
  • 19

2 Answers2

3

Try using sed

sed -nr '/-{3,}/h; /Path\s*:/H; /Everyone\s+Allow\s+FullControl/{x;G;p}' file

Output:

    -----------------------
    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\balco
             Everyone Allow  FullControl
    -----------------------
    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\BCDRPlanning
    Access : Everyone Allow  FullControl

Short Description

sed -nr '
    /-{3,}/h;            # if '---' pattern is found in a line, copy it to hold space
    /Path\s*:/H;         # if 'Path  :' pattern is found in a line, append it to hold space
    /Everyone\s+Allow\s+FullControl/{x;G;p}    # if 'Everyone Allow  FullControl' pattern is found in a line, 1) exchange Pattern and Hold space, 2) Append pattern space to hold space and 3) print pattern space.
' file

Here we make use of Pattern Space and Hold Space manipulator of sed

Snippet from man sed

   h H    Copy/append pattern space to hold space.   
   g G    Copy/append hold space to pattern space.
   x      Exchange the contents of the hold and pattern spaces.
jkshah
  • 11,387
  • 6
  • 35
  • 45
  • Works perfectly. Incredible how easy you make it seem, could you explain me a little the syntax? – Eduardo Nov 19 '13 at 05:59
  • @Eduardo Added short description. Please check. – jkshah Nov 19 '13 at 06:10
  • @Eduardo Glad that it worked for you! Some more references: [SO answer](http://stackoverflow.com/a/12834372/526471), [Examples for Sed Hold and Pattern Buffer Operations](http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-7-examples-for-sed-hold-and-pattern-buffer-operations/) – jkshah Nov 19 '13 at 06:33
2

You can do some like this with awk

awk '/Everyone Allow/ && /FullControl/' RS="-" file

    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\balco
    Access : Everyone Allow  268435456
             Everyone Allow  FullControl


    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\BCDRPlanning
    Access : Everyone Allow  FullControl
             Everyone Allow  268435456

To restore separator:

awk '/Everyone Allow/ && /FullControl/' RS="-" file | awk '/[0-9]+/ && /Everyone/ {next} $0=="" {$0="-----------------------"}1'
-----------------------
    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\balco
             Everyone Allow  FullControl

-----------------------
    Path   : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\BCDRPlanning
    Access : Everyone Allow  FullControl
Jotne
  • 40,548
  • 12
  • 51
  • 55