Echo final value of long line
Hi guys
I am writing a command to return the last value entry within a log file: Code:
[apmuser@wycvlwebh017 logs]$ less localhost_access_log.2018-10-17.txt | sort -rn | head -n 1 Code:
10.218.182.157 - - [17/Oct/2018:13:42:34 +0000] "POST /HvpWorker/hvpCommand?hvpJobId=09000002802b7496 HTTP/1.1" 200 1525 2419 Thanks in advance |
One simple way would be with cut command.
If number of items can vary from lines to lines, I can suggest you awk because it's easy to access the last item with it, otherwise you need to reverse the line with rev and cut the 1st item and re-rev the line). Can you try something and tell us? |
Pipe your output to:
Code:
sed -e 's|^.*\s\(.*\)$|\1|' |
Fantastic it worked thanks guys :D... Can you just explain what sed -e 's|^.*\s\(.*\)$|\1|' is doing?
|
The ^ just marks the beginning of a line. .* says match any character zero or more times. \s says match a white space. \( marks the beginning of a lookback section. Again .* says match anything zero or more times. \) marks the end of the lookback. $ marks the end of the line. Sed regex is greedy. So the .* grabs everything it can, but \s.* says to keep the last whitespace delimited bunch of characters separate. That is then the only thing we print out with the \1 back reference.
|
Hi guys
Something I just thought, the file name will change depending on the date etc. localhost_access_log.2018-10-17.txt and now today it is localhost_access_log.2018-10-18.txt. How in a script do we determine to search for the log file by date? Would we be using the date command? Something along the lines of Code:
if localhost_access_log. = date Cheers |
Quote:
Code:
sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | head -n 5 | sed -e 's|^.*\s\(.*\)$|\1|' | less -S |
Thank you all so much
|
New requirement from the team!
They are now saying they want the lines which include 10.218.182.157 - - [18/Oct/2018:09:42:27 +0000] "POST /HvpWorker/hvpCommand HTTP/1.1" 200 172 1 to be removed. I have tried using Code:
sed -e "/HTTP/d" I believe they want to see the first 5 lines of Code:
10.218.182.157 - - [18/Oct/2018:09:42:29 +0000] "POST /HvpWorker/hvpCommand?hvpJobId=09000002802b7614 HTTP/1.1" 200 1525 2433 Code:
sort -rn $(ls -t localhost_access_log.*.txt | head -n 1) | sed -e "/HTTP/d" But this comes back blank. I'm assuming the first sed will need to come before the second head? |
Code:
sed -n '/^10\.218\.182\.157.*HTTP/p' localhost_access_log.txt |
Quote:
Code:
sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | sed '/10.218.182.157 - - \[18\/Oct\/2018:09:42:27 +0000\] "POST \/HvpWorker\/hvpCommand?hvpJobId=09000002802b7614 HTTP\/1.1" 200 172 1/d' | less -S |
Hi bradvan
Sorry probably didn't explain that very well. Code:
[apmuser@wycvlwebh017 logs]$ sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | head -n 5 Code:
sed -e 's|^.*\s\(.*\)$|\1|' However now I have been told we don't need the following lines(In Bold) Code:
10.218.182.157 - - [18/Oct/2018:10:42:40 +0000] "POST /HvpWorker/hvpCommand?hvpJobId=09000002802b7635 HTTP/1.1" 200 1525 2347 Thanks in advance |
I've worked out how to do it as such
Code:
[apmuser@wycvlwebh017 logs]$ sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | head -n 10 | grep JobId |
Quote:
Code:
sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | sed -r '/HTTP\/1\.1" [0-9]+ [0-9]+ [0-9]$/d' | head -n 5 | less -S |
Quote:
Code:
sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | head -n 10 | sed -r '/JobId/!d;s/^.*\s(.*)$/\1/' |
All times are GMT -5. The time now is 04:44 AM. |