简体   繁体   English

使用perl单线使用regex测距法来获取第一个字段和最后一个字段

[英]Grabbing first and last field with regex delimeter using perl one-liner

I need to parse a file and grab certain fields from it using a regular expression as part of the delimiter. 我需要使用正则表达式作为分隔符的一部分来解析文件并从中获取某些字段。 I thought I can use perl to do this(?). 我以为可以用perl来做到这一点(?)。 The problem is I can't get it to work properly. 问题是我无法使其正常工作。 Here's a one liner which I thought would allow me to print fields that are separated by one more white spaces (in this case one or more space): 我想这是一种衬纸,我希望它可以打印被一个以上空白(在本例中为一个或多个空格)分隔的字段:

bash_prompt> perl -anF'/ +/' -e 'print "$F[0], $F[-1]\n"' build_outputfile

The output file is from a makefile. 输出文件来自makefile。

Here, I want to print out the first token, and the last token. 在这里,我要打印第一个令牌和最后一个令牌。 So in my case which compiler was used and which file was compiled. 因此,在我的情况下,使用哪个编译器和哪个文件进行编译。 Perhaps there's a better way to do it, but now I'm bothered as to why my perl one liner does not work. 也许有更好的方法可以执行此操作,但是现在我对为什么我的perl one衬垫无法正常工作感到困扰。

Anyways, the regular expression '/ +/ does not appear to work. 无论如何,正则表达式'/ + /似乎不起作用。 I get some unexpected output. 我得到一些意外的输出。 Perhaps F does not actually want a regular expression? 也许F实际上不想要正则表达式? When I replace F's argument with '/ /' that contains one space, I still don't get a expected output. 当我用包含一个空格的'/ /'替换F的参数时,仍然没有得到预期的输出。

Can anyone help? 有人可以帮忙吗? Thanks. 谢谢。

Here's some test code for you to try. 这是一些测试代码供您尝试。 Save it in a file: 将其保存在文件中:

g++ -c  -g  -Wall -I/codedir/src/CanComm/include -I/home/codemonkey/workspace/thirdparty/Boost -Wno-deprecated SCMain.cpp
g++ -c -g  -Wall -I./object/include -I./wrapper/include -I./Properties/include -I./Messaging/include -I/codedir/src/Logging/sclog/include ./object/SCObject.cpp ./object/RandNumGenerator.cpp ./object/ScannerConstraints.cpp ./object/ThreadSync.cpp ./object/SCData.cpp ./object/AirScanData.cpp ./object/ClusterData.cpp ./object/WarmupData.cpp ./object/SCCommand.cpp ./object/ScanCommands.cpp ./object/RCCommands.cpp ./object/ReconData.cpp ./object/UICommTool.cpp ./object/UIMsg.cpp ./object/UI2SCConversion.cpp ./object/RCMsg.cpp ./object/RCMessageInfo.cpp ./object/Utils.cpp ./object/ZBackupTable.cpp ./object/ZBackupFactory.cpp
g++ -c -g  -Wall -I./Properties/include -I/codedir/src/Logging/sclog/include -I./object/include -I/home/codemonkey/workspace/thirdparty/Boost  ./Properties/PropertyMap.cpp

According to perldoc perlrun : 根据perldoc perlrun

-Fpattern -F模式

specifies the pattern to split on if -a is also in effect. 如果-a也有效,则指定要分割的模式。 The pattern may be surrounded by "//", "", or '', otherwise it will be put in single quotes. 该模式可以用“ //”,“”或“”括起来,否则将用单引号引起来。 You can't use literal whitespace in the pattern. 您不能在模式中使用文字空白。

I have to admit: What a thoroughly arbitrary restriction! 我必须承认:多么彻底的任意限制!

对于您的问题,您实际上不需要将模式指定为默认值,空格可以使您足够好。

perl -anle 'print "$F[0], $F[-1]"' build_outputfile

Your Regex pattern should be like this: 您的Regex模式应如下所示:

'/\s+/'

\\s means to match any whitespace \\ s表示匹配任何空格

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

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