简体   繁体   English

Regex / grep查找X深度的文件系统路径

[英]Regex / grep to find filesystem path of X depth

I need a regex/grep/sed/whatever expression that will match 我需要一个正则表达式/ grep / sed /任何匹配的表达式

"F:\vol2\home\USERNAME" /grant ... 

and not match on anything like these 并且不匹配任何类似的东西

"F:\vol2\home\USERNAME\subfolder" /grant ...
"F:\vol2\subfolder\subfoler2" /grant ...
"F:\vol2\home" /grant

Obviously, 'USERNAME' is a variable and needs to be treated as such. 显然,'USERNAME'是一个变量,需要这样对待。 I was thinking something like 'home\\\\[A-Za-z]*[^\\\\]' but that's obviously not working. 我在想'home \\\\ [A-Za-z] * [^ \\\\]',但这显然不起作用。

awk '/home\\[^\\]+\"/{print $1}'

seems to work. 似乎工作。

In perl, this works too: 在perl中,这也有效:

while (<>) {
  print if (/\"F:\\vol2\\home\\[^\\]+\"/);
}
awk -F '\\' -v user=USERNAME 'NF==4 && $3=="home" && $4 ~ "^"user'

在反斜杠上分裂,必须有4个字段,第三个是“home”,第四个字段以用户变量开头

grep'\\ vol2 \\ home \\'$ {USERNAME}'''x2

If your paths are always the first field, 如果您的路径始终是第一个字段,

awk '$1~/home\\[^\\]+\"/{print $1}'

Or you can split the first field on \\ and check number of tokens to be 4 或者您可以在\\上拆分第一个字段,并检查令牌数为4

$ ruby -ane 'puts $F[0] if $F[0].split("\\").size==4 && /home/' file
"F:\vol2\home\USERNAME"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM