hi,
I need to get a string from a line matching the last occurrence of another string in a log file. I can get the last occurrence of in the log file like this:
perl -ne '$string=$_ if /Successfully sent/; END{print $string}' $log
+file
The line something like this:
2017-11-02 12:08:11,120 INFO [Timer-Driven Process Thread-8] o.a.nifi.
+remote.StandardRemoteGroupPort RemoteGroupPort[name=Receiver,targets=
+https://host.sub.domain.tld:9443/nifi] Successfully sent [StandardFlo
+wFileRecord[uuid=xxxxxxxxxxxx,claim=StandardContentClaim [resourceCla
+im=StandardResourceClaim[id=xxx, container=default, section=xxx], off
+set=384244, length=163820],offset=0,name=xxxxx.json,size=163820]] (15
+9.98 KB) to https://host.sub.domain.tld:9443/nifi-api in 242 millisec
+onds at a rate of 660.28 KB/sec
Now I need to get just the timestamp at the beginning of this line. This is how I thought it should work:
perl -ne '$string=$_ if /Successfully sent/; $string =~ /^(.*) INFO.*
+$/; END{print $1}' $logfile
But no success until now.
Any tips greatly appreciated. Thanks in advance!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.