在这种情况下我需要更多的帮助,做一些类似于两种模式之间的grep: 我需要寻找Everyone Allow FullControl
说我有这个:
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
-----------------------
所以我会得到类似的东西:
Path : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\balco
Everyone Allow FullControl
-----------------------
Path : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\BCDRPlanning
Access : Everyone Allow FullControl
我不知道该怎么做所以我来这里
答案 0 :(得分:3)
尝试使用sed
sed -nr '/-{3,}/h; /Path\s*:/H; /Everyone\s+Allow\s+FullControl/{x;G;p}' file
输出:
-----------------------
Path : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\balco
Everyone Allow FullControl
-----------------------
Path : Microsoft.PowerShell.Core\FileSystem::\\eiesc1\BCDRPlanning
Access : Everyone Allow FullControl
简短说明
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
这里我们使用sed
来自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.
答案 1 :(得分:2)
您可以使用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
恢复分隔符:
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