grep을 사용하여 명령 출력에서 ​​특정 변수를 인쇄하는 방법

grep을 사용하여 명령 출력에서 ​​특정 변수를 인쇄하는 방법

test.txt 파일:

Ext Temperatur:  210.0°
Avg.Speed(All):    62.89mm/s
Avg.Speed(Print):  49.99mm/s
Avg.Speed(Travel): 199.84mm/s
Overall Time (w/o Acceleration): 11:00:41 (39640.65sec)

PHP 코드를 사용하여 출력을 39640.65sec잡아서 인쇄하려면 어떻게 해야 합니까 ?39640.65grep

이 코드를 사용하고 있지만 다음과 같이 인쇄됩니다.11:00:41 (39640.65sec)

<?php
$gcode_new = "grep -Po '(?<=Overall Time \(w/o Acceleration\):   ).*' test.txt";

include('Net/SSH2.php');

$ssh = new Net_SSH2('XXX.61.XXX.227');
if (!$ssh->login('root', 'PASS')) {
    exit('Login Failed');
}
$target = $ssh->exec($gcode_new);

echo $target ;
?>

답변1

다음을 시도해 볼 수 있습니다.

grep -Po "[.0-9]*(?=sec\))"

산출:

39640.65

답변2

파일에 여러 줄이 있는 경우:

파일에서 첫 번째 항목을 얻으려면 이것을 사용하십시오.

grep -m1 -Po "[.0-9]*(?=sec\))" testing.txt

파일에서 마지막 항목을 얻으려면 다음을 수행하십시오.

grep -Po "[.0-9]*(?=sec\))" testing.txt | tail -1

관련 정보