On Wed, 20 Jan 2021 15:49:49 +0000, Pancho wrote:
> On 20/01/2021 13:02, Martin Gregorie wrote:
>
>> So, questions for Pancho:
>>
>> - how did you create your input files?
>>
>> - Is there any chance that they don't contain any 'newline' characters?
>>
>> I'm asking because its quite possible that awk would stall, waiting for
>> a the next character, if it had read several KB of data without finding
>> a newline character or EOF.
>>
>>
>>
> I used:
>
> tail -f testfile | mawk...
>
> I used vi to generate files, with newlines, which I appended into the
> testfile, or just echo "stuff..." appended into the test file.
>
> But Richard's example is perfect to demonstrate the problem:
>
> >> $ (seq 9999 | head -c 4095; sleep 2; echo) | mawk '{print}'
>
> Not only does seq quickly generate a byte stream, but it allows you to
> see exactly what byte you are on. I'm not sure why he used the brackets
> () but I left them in as they don't hurt.
>
> I guess he used the echo because he is habitually tidy :-).
>
> Maybe the answer is that for reliability I should have used perl instead
> of awk?
>
Dunno about that: awk takes a bit of getting used to because its a
specialised tool for extracting data from text files: I suggested using
it in this case because its brilliant for tasks like scanning log files
for a set of error messages and doing appropriate stuff for each one it
spots. As you've seen, the code needed to recognise 'battery low'
warnings in, say, /var/log/messages and issue a 'shutdown -h now' command
if one is found would be a trivially small program because it
automatically reads lines and splits them into words before using a regex
to trigger actions on any lines that the regex matches: about all you
need to write is the regexes and the code that each of them triggers.
By comparison C, Java, Perl or Python, ... are all general purpose
programming languages and so you need to code the file reading loop and
(probably) the scan routine that recognises interesting lines in the file
as well as the code to do something useful when a line is recognised.
IOW, to spot a 'battery low' message in the /var/log/messages logfile and
issue a STOP command the Raspbian would be a one liner in awk:
awk -- '/battery low/ { system("shutdown -h now") }' /var/log/messages
as compared with at least 10-20 lines in any of the other languages I
mentioned - and they'll take longer to write simply because the loop and
the line parser will need to be coded and debugged.
The above, of course, is assuming that you don't want to simply build the
shutdown command into the program that's watching battery voltage
A lot of programmers would do it that way while others, of which I'm one,
like to keep different activities in single purpose programs - and,
though its unlikely in this particular case, would keep them separate
because you might someday need to let another program trigger the boojum
as well as the one you're designing right now.
> Maybe perl is more standard?
>
Dunno. These days I bet more people would try to write it in Python or
Javascript than would use Perl.
--
Martin | martin at
Gregorie | gregorie dot org
--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)
|