Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex/grep查找X深度的文件系统路径_Regex_String_Bash_Sed_Grep - Fatal编程技术网

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

Regex/grep查找X深度的文件系统路径,regex,string,bash,sed,grep,Regex,String,Bash,Sed,Grep,我需要一个regex/grep/sed/任何匹配的表达式 "F:\vol2\home\USERNAME" /grant ... 而不是像这样的比赛 "F:\vol2\home\USERNAME\subfolder" /grant ... "F:\vol2\subfolder\subfoler2" /grant ... "F:\vol2\home" /grant 显然,“USERNAME”是一个变量,需要这样对待。我在想类似于“home\\[A-Za-z]*[^\\\]”的东西,但这显然不起

我需要一个regex/grep/sed/任何匹配的表达式

"F:\vol2\home\USERNAME" /grant ... 
而不是像这样的比赛

"F:\vol2\home\USERNAME\subfolder" /grant ...
"F:\vol2\subfolder\subfoler2" /grant ...
"F:\vol2\home" /grant
显然,“USERNAME”是一个变量,需要这样对待。我在想类似于“home\\[A-Za-z]*[^\\\]”的东西,但这显然不起作用

awk '/home\\[^\\]+\"/{print $1}'
似乎有效

在perl中,这也适用:

while (<>) {
  print if (/\"F:\\vol2\\home\\[^\\]+\"/);
}
while(){
如果(/\“F:\\vol2\\home\\[^\\]+\”/)打印;
}

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


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

如果路径始终是第一个字段

awk '$1~/home\\[^\\]+\"/{print $1}'
或者您可以拆分
\
上的第一个字段,并检查令牌数是否为4

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