grep 不返回所有匹配的行
grep not returning all matching lines
我不知道如何让 grep return 在文件中找到所有行。它计算出正确的数字,但只打印最后一个匹配项。
$found = array();
$term = escapeshellarg(self::$term);
foreach(self::$files as $file)
{
if($count = exec("grep -i -c ".$term." ".$file))
{
$lines = exec("grep -i -n ".$term." ".$file);
$found[] = array('count'=>$count,'lines'=>$lines,'file'=>str_replace(self::$dir, '', $file));
}
}
self::$files = $found;
假设 self::$term = 'console.log'
。它在一个特定文件中找到三个匹配项,这是正确的(该术语出现在测试文件的第 16、21 和 31 行),但 $lines
仅打印:31: console.log(response);
.
我错过了什么?
来自 php manual 执行:
Return Values
The last line from the result of the command. If you need to execute a
command and have all the data from the command passed directly back
without any interference, use the passthru() function.
所以你只会得到 grep 找到的最后一个匹配项,因为它是输出的最后一行。
希望对您有所帮助。
$result = array();
exec("grep '"$yourstring"' ./update.php | cut -d':' -f1 | sed -e 's/^[ ]*//'", $result);
print_r($result);
我不知道如何让 grep return 在文件中找到所有行。它计算出正确的数字,但只打印最后一个匹配项。
$found = array();
$term = escapeshellarg(self::$term);
foreach(self::$files as $file)
{
if($count = exec("grep -i -c ".$term." ".$file))
{
$lines = exec("grep -i -n ".$term." ".$file);
$found[] = array('count'=>$count,'lines'=>$lines,'file'=>str_replace(self::$dir, '', $file));
}
}
self::$files = $found;
假设 self::$term = 'console.log'
。它在一个特定文件中找到三个匹配项,这是正确的(该术语出现在测试文件的第 16、21 和 31 行),但 $lines
仅打印:31: console.log(response);
.
我错过了什么?
来自 php manual 执行:
Return Values
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
所以你只会得到 grep 找到的最后一个匹配项,因为它是输出的最后一行。
希望对您有所帮助。
$result = array();
exec("grep '"$yourstring"' ./update.php | cut -d':' -f1 | sed -e 's/^[ ]*//'", $result);
print_r($result);