简体   繁体   English

试图从 grep 文件路径 pipe output

[英]trying to grep file paths from pipe output

I need to find the file paths in perforce which are not following some standard we following.我需要在 perforce 中找到不遵循我们遵循的某些标准的文件路径。

Basically, Our standard way to add new file in perforce path:- //depot/project/name/content/<sub_project>/<version>/...基本上,我们在 perforce 路径中添加新文件的标准方法:- //depot/project/name/content/<sub_project>/<version>/...

Here <sub_project> should be of alpha numeric and <version> should be of only integer kind eg:- 1.0, 1.1...etc.这里<sub_project>应该是字母数字,而<version>应该只是integer 类型,例如:- 1.0、1.1...等。 So I need to find the files which are not following above standard.所以我需要找到不符合上述标准的文件。 And here is my command where I could get the output paths which are following correct standard.这是我的命令,我可以获得遵循正确标准的 output 路径。 Is this correct way of using egrep here?这是在这里使用egrep的正确方法吗?

p4 files //depot/project/name/content/... | egrep "//depot/project/name/content/.+/[[:alnum:]]+"

Lets say we have following lines from p4 command ouptput:-假设我们从 p4 命令输出中有以下几行:-

//depot/project/name/content/cuda/sccm_2.1
//depot/project/name/content/cpla/test_3.1
//depot/project/name/content/ctest/arm_test
//depot/project/name/content/bfm/1.2
//depot/project/name/content/nvlog/1.0

I am interested only in first three paths ie我只对前三个路径感兴趣,即

//depot/project/name/content/cuda/sccm_2.1
//depot/project/name/content/cpla/test_3.1
//depot/project/name/content/ctest/arm_test

It's not clear to me if you want to consider versions like 1 or 1.2.3 as invalid or not.我不清楚您是否想将11.2.3等版本视为无效。 This treats both of those as invalid and requires version number to have exactly one .这会将这两个都视为无效,并要求版本号恰好有一个. . . It is easy to modify the regex if needed:如果需要,很容易修改正则表达式:

awk '$NF !~ /^[0-9]*\.[0-9]*$/' FS=/ input

Since you didn't give a list of possible names, I created a sample list:由于您没有提供可能名称的列表,因此我创建了一个示例列表:

echo "//depot/project/name/content/gaga/1.1\n//depot/project/name/content/chomp{}/1.1\n//depot/project/name/content/kaka/99.7\n//depot/project/name/content/kuku/1"    

//depot/project/name/content/gaga/1.1 //仓库/项目/名称/内容/gaga/1.1
//depot/project/name/content/chomp{}/1.1 //depot/project/name/content/chomp{}/1.1
//depot/project/name/content/kaka/99.7 //depot/project/name/content/kaka/99.7
//depot/project/name/content/kuku/1 //仓库/项目/名称/内容/kuku/1

To find the 2 matches I used grep -p (because perl regexp is much more friendly)为了找到 2 个匹配项,我使用grep -p (因为 perl 正则表达式更友好)

echo "//depot/project/name/content/gaga/1.1\n//depot/project/name/content/chomp{}/1.1\n//depot/project/name/content/kaka/99.7\n//depot/project/name/content/kuku/1" | grep -P "//depot/project/name/content/\w+/\d+\.\d+"

//depot/project/name/content/gaga/1.1 //仓库/项目/名称/内容/gaga/1.1
//depot/project/name/content/kaka/99.7 //depot/project/name/content/kaka/99.7

Now, if your version might be missing the dot, you can change the regexp to现在,如果您的版本可能缺少点,您可以将正则表达式更改为
"//depot/project/name/content/\w+/\d+\.?\d*"

Last, but not least - if you already called the p4 command with the full path, you can probably ignore the path in the regexp, as it is given by you...最后但并非最不重要的一点 - 如果您已经使用完整路径调用了 p4 命令,您可能会忽略正则表达式中的路径,因为它是由您给出的......

UPDATE更新
given the input you gave, updated the regexp to给定您提供的输入,将正则表达式更新为
grep -P "//depot/project/name/content/\w+/[a-zA-Z]\w+(\d\.\d+)?"
If file names might begin with none alphabet signs, add them to the square brackets.如果文件名可能不以字母符号开头,请将它们添加到方括号中。

> echo "//depot/project/name/content/cuda/sccm_2.1\n//depot/project/name/content/cpla/test_3.1\n//depot/project/name/content/ctest/arm_test\n//depot/project/name/content/bfm/1.2\n//depot/project/name/content/nvlog/1.0" | grep -P "//depot/project/name/content/\w+/[a-zA-Z]\w+(\d\.\d+)?"
//depot/project/name/content/cuda/sccm_2.1
//depot/project/name/content/cpla/test_3.1
//depot/project/name/content/ctest/arm_test

The following grep command uses a regex that matches your desired path, but also uses the -v option to invert the match.以下 grep 命令使用与所需路径匹配的正则表达式,但也使用-v选项来反转匹配。 This has the effect of returning the unwanted paths:这具有返回不需要的路径的效果:

grep -v -E "\/\/depot\/project\/name\/content\/[[:alnum:]]*\/([0-9]+\.?)*[0-9]+"

The regular expression doesn't permit the <version> to start or end with a .正则表达式不允许<version>以 . 开头或结尾. . . Also, [:alnum:] doesn't include _ or - , so those would have to be added if needed.此外, [:alnum:]不包括_- ,因此如果需要,必须添加它们。

So from this data:所以从这个数据:

//depot/project/name/content/cuda/sccm_2.1
//depot/project/name/content/cpla/test_3.1
//depot/project/name/content/ctest/arm_test
//depot/project/name/content/bfm/1.2
//depot/project/name/content/nvlog/1.0
//depot/project/name/content/nvlog/.0
//depot/project/name/content/bfm/10.2
//depot/project/name/content/bfm/10.2.1.7
//depot/project/name/content/nvlog/123545

It will return:它将返回:

//depot/project/name/content/cuda/sccm_2.1
//depot/project/name/content/cpla/test_3.1
//depot/project/name/content/ctest/arm_test
//depot/project/name/content/nvlog/.0

I think that's what you want, but if not, let me know.我想这就是你想要的,但如果不是,请告诉我。

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

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