简体   繁体   English

Windows中的EGREP和AWK等效命令

[英]EGREP and AWK equivalent command in Windows

I'm building an automated pre-commit script based in the Carlos Buenos Vinos tutorial , but the BIG problem is: I work in a company that use Windows in the dev computers (I know I know and I can't do nothing about this sry). 我正在基于Carlos Buenos Vinos教程构建一个自动化的预提交脚本,但是最大的问题是:我在一家在开发计算机中使用Windows的公司工作(我知道我知道,对此我无法采取任何措施。对)。

The script has the following method: 该脚本具有以下方法:

private function extractCommitedFiles()
{
    $output = array();
    $rc = 0;

    exec('git rev-parse --verify HEAD 2> /dev/null', $output, $rc);

    $against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
    if ($rc == 0) {
        $against = 'HEAD';
    }

    // unix / linux
    // exec("git diff-index --cached --name-status $against | egrep '^(A|M)' | awk '{print $2;}'", $output);
    // windows
    exec("git diff-index --cached --name-status $against | egrep '^(A|M)' | awk '{print $2;}'", $output);

    return $output;
}

So... does anybody have an idea to an equivalent command to the line bellow? 那么...有人对波纹管的等效命令有任何想法吗?

exec("git diff-index --cached --name-status $against | egrep '^(A|M)' | awk '{print $2;}'", $output);

I must have the following result if I run this command in UNIX/Bash: 如果我在UNIX / Bash中运行此命令,则必须得到以下结果:

core/web/favicon.ico                        
core/web/htaccess.txt                       
core/web/index.html                         
core/web/slim.php                           
core/web/swagger/css/print.css              
core/web/swagger/css/reset.css              
core/web/swagger/css/screen.css             
core/web/swagger/css/style.css              
core/web/swagger/css/typography.css         
core/web/swagger/fonts/DroidSans-Bold.ttf        

But using exec('... without the egrep and awk commands I'm having the following results: 但是使用exec('......而没有egrep和awk命令,我得到以下结果:

A       core/web/htaccess.txt                      
A       core/web/index.html                        
A       core/web/slim.php                          
A       core/web/swagger/css/print.css             
A       core/web/swagger/css/reset.css             
A       core/web/swagger/css/screen.css            
A       core/web/swagger/css/style.css             
A       core/web/swagger/css/typography.css        
A       core/web/swagger/fonts/DroidSans-Bold.ttf   

Thank u guys! 谢谢你们!

First of all, that command is a bad example. 首先,该命令是一个不好的例子。 It is hardly ever necessary to use grep and awk in the same pipeline, since awk is vastly more powerful. 几乎没有必要在同一管道中使用grepawk ,因为awk功能要强大得多。 So, the command should be shortened to 因此,该命令应缩短为

 awk '/^(A|M)/ {print $2}'

Secondly, even that is a bad example, since git diff-index has various output options, including --name-only and --diff-filter , which can be combined as 其次,即使这是一个不好的例子,因为git diff-index具有各种输出选项,包括--name-only--diff-filter ,它们可以组合为

git diff-index --cached --name-only --diff-filter=AM $against

You can download windows versions of the tools and add the bin folder to the PATH variable. 您可以下载Windows版本的工具,然后将bin文件夹添加到PATH变量中。

http://gnuwin32.sourceforge.net/packages/gawk.htm http://gnuwin32.sourceforge.net/packages/grep.htm http://gnuwin32.sourceforge.net/packages/gawk.htm http://gnuwin32.sourceforge.net/packages/grep.htm

For "egrep" you can use "grep -E". 对于“ egrep”,可以使用“ grep -E”。 "gawk" will do the job exactly like "awk" in your case. 在您的情况下,“ gawk”将完全像“ awk”一样完成工作。

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

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