I am very new to php. I\'d really appreciate all the help here.
I am using sftp to login to a server and get a file. I am only interested in the last line in that file.
$data = explode("\n", $data);
$last_line = end($data);
$parts = explode("\t", $last_line);
Might be overkill if the file is small, but wrote this function a while ago. Returns the last n lines of a file/stream.
function tail($file, $lines = 10, $buffer = 4096)
{
if(is_resource($file) && (get_resource_type($file) == 'file' || get_resource_type($file) == 'stream'))
$f = $file;
elseif(is_string($file))
$f = fopen($file, 'rb');
else
throw new Exception('$file must be either a resource (file or stream) or a filename.');
$output = '';
$chunk = '';
fseek($f, -1, SEEK_END);
if(fread($f, 1) != "\n")
$lines -= 1;
while(ftell($f) > 0 && $lines >= 0)
{
$seek = min(ftell($f), $buffer);
fseek($f, -$seek, SEEK_CUR);
$output = ($chunk = fread($f, $seek)).$output;
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
$lines -= substr_count($chunk, "\n");
}
while($lines++ < 0)
$output = substr($output, strpos($output, "\n")+1);
fclose($f);
return $output;
}
If tab separated, simply do a split and assign what you want to variables.
$columns = explode("\t",$line);
$foo = $columns[15];
...
Why use php?
With input file:
7 1 0 59422170 306669 20188 20386 0 0 39787 59981 2014 67796 48953 2 7 90 1 1.81 11.3 12:19:50
and command line:
tail -n1 myfile | sed 's/\s\s*/ /g' | cut -d' ' -f15,16,19,20,21
result is:
2 7 1.81 11.3 12:19:50