开发者

Regex / grep to find filesystem path of X depth

I need a regex/grep/sed/whatever expression that will matc开发者_运维问答h

"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. I was thinking something like 'home\\[A-Za-z]*[^\\]' but that's obviously not working.


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

seems to work.

In perl, this works too:

while (<>) {
  print if (/\"F:\\vol2\\home\\[^\\]+\"/);
}


awk -F '\\' -v user=USERNAME 'NF==4 && $3=="home" && $4 ~ "^"user'

splitting on a backslash, there must be 4 fields, the third is "home" and the fourth begins with the user variable


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜