简体   繁体   中英

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).

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:

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:

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. 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 --cached --name-only --diff-filter=AM $against

You can download windows versions of the tools and add the bin folder to the PATH variable.

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

For "egrep" you can use "grep -E". "gawk" will do the job exactly like "awk" in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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