Extract lines if string found from last 30 min only


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract lines if string found from last 30 min only
# 15  
Old 02-16-2019
Quote:
Originally Posted by nezabudka
Thank you very much for the comments. All the above I be taken into account for the future.
And in the last remark. This is my carelessness and bug. The order of the expressions was violated.
Apparently I wanted to make something like that.
Code:
        if ($7 == "PM") a[1]+=12
        if (a[1] == 24) a[1] = "00"
        $5 = a[1] ":" a[2] ":" a[3]

... ... ...
Thank you for teaching, it was very informative.
Hi nezabudka,
I'm afraid the above code still doesn't work for anything that started with a[1]==12. If you start with 12 AM on a 12 hour clock you should end up with hour 00 on a 24 hour clock (the above code ends up with hour 12) and if you start with 12 PM on a 12 hour clock you should end up with hour 12 on a 24 hour clock (the above code ends up with hour 00).

If you don't like the code I suggested in post #9 or either of the suggestions I made in post #12 you could also try:
Code:
        $5 = (($7 == "PM") ? a[1] + 12 * (a[1] != 12) : (a[1] == 12) ? "00" : a[1]) ":" a[2] ":" a[3]

This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 03-09-2019
Hello,

Thanks to everyone for their efforts. Sorry, was away for few days and didn't get the time to look at the solutions provided.

I tried all scripts from this forum but none of the script worked. All the scripts fetching the lines for entire day instead of last 30 min. My requirement is to pull the lines for last 30 min only.

Script used (for e.g.) :


Code:
awk -F "<|>| |, |," -v d="$(LANG=C date -d -30minutes -u +"%Y%m%d%T")" '
BEGIN   { split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", m2b)
        for(i = 1; i <= 12; i++)
        b2m[m2b[i]] = sprintf("%02d", i)
}
/^</    { line=$0
        if ( length($3) < 2 ) $3 = "0" $3
        split($5, a, ":" s)
        if ($5 == 24) $5 = "00"
        if ($7 == "PM") $5 = (a[1]+=12) ":" a[2] ":" a[3]
        $0 = $4 b2m[$2] $3 $5
        if ( d < $0 ) print line
}
' file_1.out

Code:
$ date   ############ current Date/time on Linux when I ran the script

Sat Mar  9 18:53:47 UTC 2019



Output from the above script:

Code:
<Mar 9, 2019, 1:02:39,176 AM UTC> <Error> <Coherence> <BEA-000000>

<Mar 9, 2019, 1:13:22,583 AM UTC> <Error> <Coherence> <BEA-000000>
<Mar 9, 2019, 1:47:08,198 AM UTC> <Error> <Coherence> <BEA-000000>
<Mar 9, 2019, 5:16:42,24 AM UTC> <Error> <Coherence> 

<Mar 9, 2019, 6:50:41,556 PM UTC> <Error> <Coherence> <BEA-000000>
<Mar 9, 2019, 6:56:45,132 PM UTC> <Error> <Coherence> <BEA-000000>


Please suggest as I need to pull last 30 min lines only whenever i execute this script not for the entire day.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-09-2019 at 03:35 PM.. Reason: Added CODE tags.
# 17  
Old 03-09-2019
How about carefully reading, understanding, and heeding all the posts (and comments therein) offering help to you? The script you used was commented on and improved in a later post. You shouldn't expect turnkey solutions (although those are frequently delivered) but understand the proposals and experiment with them until they satisfy your needs.


Having said that, how about
Code:
$ paste -d'\t\b' <(date -f <(sed 's/^<\|>.*$//g; s/,//2' file) +"%F %T") file | awk -F"\t" -vTS="$(date -d'30 min ago' +'%F %T')" '($1 > TS) {sub ("^" $1 FS, ""); print}'
<Mar 9, 2019, 6:50:41,556 PM UTC> <Error> <Coherence> <BEA-000000>
<Mar 9, 2019, 6:56:45,132 PM UTC> <Error> <Coherence> <BEA-000000>

# 18  
Old 03-10-2019
Hi RudiC,

Yes indeed, I have checked all posts before replying back and clearly mentioned that none of the <scripts> worked because I tried all of them. The previous post had just one of the example as don't want to bump over with all outcomes as they all produced the same outcome.

Intention here is to resolve the issue to get experts advise to get issue resolved as pulling data with dates are extremely difficult due to presence of >=2 date formats in log file.


Below script result into invalid date
Code:
$ paste -d'\t\b' <(date -f <(sed 's/^<\|>.*$//g; s/,//2' file) +"%F %T") file | awk -F"\t" -vTS="$(date -d'30 min ago' +'%F %T')" '($1 > TS) {sub ("^" $1 FS, ""); print}'

date: invalid date `2019-03-04T11:03:16.576+0000: 1392540.816: [GC [PSYoungGen: 934720K-'
date: invalid date `\tat java.lang.reflect.Method.invoke(Method.java:606)'


Last edited by Don Cragun; 03-14-2019 at 04:56 PM.. Reason: Add CODE tags again.
# 19  
Old 03-14-2019
Quote:
Originally Posted by rockstar
Hi RudiC,

Yes indeed, I have checked all posts before replying back and clearly mentioned that none of the <scripts> worked because I tried all of them. The previous post had just one of the example as don't want to bump over with all outcomes as they all produced the same outcome.

Intention here is to resolve the issue to get experts advise to get issue resolved as pulling data with dates are extremely difficult due to presence of >=2 date formats in log file.


Below script result into invalid date
Code:
$ paste -d'\t\b' <(date -f <(sed 's/^<\|>.*$//g; s/,//2' file) +"%F %T") file | awk -F"\t" -vTS="$(date -d'30 min ago' +'%F %T')" '($1 > TS) {sub ("^" $1 FS, ""); print}'

date: invalid date `2019-03-04T11:03:16.576+0000: 1392540.816: [GC [PSYoungGen: 934720K-'
date: invalid date `\tat java.lang.reflect.Method.invoke(Method.java:606)'

Hi rockstar,
I'm very happy that you had other important matters that kept you away from this thread for a few days after you had given us your assignment to work on in your absence. I'm very sorry that we were not able to give you code that worked in your unspecified environment. I apologize for not responding on this issue for the last four days, but I've also been busy doing other things.

We are here to help you learn how to write code to meet your needs on your own. We are not here to act as your unpaid programming staff and should not be expected to write code for you while you are away doing something else. If you're unwilling to answer questions, unwilling to show us the output each of the suggested responses produced on your system, and explain what was going wrong; then there is no reason for us to waste any time trying to help you learn how to do things like this on your own.

Just saying that a script doesn't work doesn't help anyone. I can easily state that some code that you have written doesn't work, but if I don't explain how it didn't work or why it didn't work none of us learns anything useful about the problem at hand.

Like I can tell you that using:
Code:
date -d @-5000 '%Y-%m-%d %H:%M:%S'

mimicking something you showed us in post #1 in this thread is wrong. But that doesn't help you learn how to fix it. The above code has absolutely nothing to do with what the time was half an hour ago. The above code asks the system to give you a time 5000 seconds before the UNIX Epoch (i.e. 5000 seconds before midnight on the morning of January 1, 1970 at 12:00:00 AM GMT). Something like:
Code:
date -d now-1800seconds '%Y-%m-%d %H:%M:%S'

would come a lot closer to giving you a timestamp that occurred 30 minutes ago (and in a format that could be used to directly compare two timestamps as strings to see if one was earlier or later than the other until we get to the year 10000).

Your repeated refusal to use CODE tags when presenting sample input, sample output, and code segments shows us that you don't want us to see the actual format of the data you are processing and makes it impossible for us to guess at how a real solution to your problem would need to be written. (The moderators have attempted to clean up your posts, but we have obviously guessed incorrectly on some of your formatting or one or more of the suggested solutions provided would likely have met your needs.)

Above you say that having >=2 date formats is a problem??? You originally said there were exactly two date formats and that one of those formats was to be completely ignored. That made things easy. If there are other date formats you haven't told us about, it becomes very clear why none of the suggested solutions had a chance of working in your environment.

The fact that the date format you have given us to work with can't be directly compared to other dates in that format between the hours of 11:30pm on one day and 1:00am on the next day nor between 11:30am and 1:00pm on the same day is a nuisance that requires the date format in your sample data to be converted to a different format for comparisons, but I thought most, if not all, of the suggestions you had been given had tried to do that (and when they didn't, follow-up comments provided ways to get around those problems).

Last edited by RudiC; 03-15-2019 at 03:57 AM.. Reason: Epoch 1870 -> 1970
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

2. Shell Programming and Scripting

Extract lines with min value, using two field separators.

I have a file with two ID columns followed by five columns of counts in fraction form. I'd like to print lines that have a count of at least 4 (so at least 4 in the numerator, e.g. 4/17) in at least one of the five columns. Input file: comp51820_c1_seq1 693 0/29 0/50 0/69 0/36 0/31... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

3. UNIX for Dummies Questions & Answers

Integrate MIN and MAX in a string

I need to use awk for this task ! input (fields are separated by ";"): 1%2%3%4%;AA 5%6%7%8%9;AA 1%2%3%4%5%6;BB 7%8%9%10%11%12;BBIn the 1st field there are patterns composed of numbers separated by "%". The 2nd field define groups (here two different groups called "AA" and "BB"). Records... (8 Replies)
Discussion started by: beca123456
8 Replies

4. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

5. Shell Programming and Scripting

Get 20 lines above string found, and 35 below string

i want to search a log for a string. when that string is found, i want to grab the a set number of lines that came before the string, and a set number of lines that come after the string. so if i search for the word "Error" in the /var/log/messages file, how can I output the 20 lines that came... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

grep log lines logged in 10 min

A log files has lines (1 line per each log for a majority; a few for 2 lines per each log) May 31 14:00:11 rtprodapp1 local2:notice sudo: jdoe : TTY=pts/0 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - May 31 14:03:19 rtprodapp1 local2:notice sudo: jdoe : TTY=pts/0 ; PWD=/home/jdoe ;... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

7. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

8. Shell Programming and Scripting

Find min.max value if matching columns found using AWK

Input_ File : 2 3 4 5 1 1 0 1 2 1 -1 1 2 1 3 1 3 1 4 1 6 5 6 6 6 6 6 7 6 7 6 8 5 8 6 7 Desired output : 2 3 4 5 -1 1 4 1 6 5 6 8 5 8 6 7 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

9. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

10. UNIX for Dummies Questions & Answers

Best approach for a 10 min extract out of several log files with timestamped records

I have a task where I need to code a shell script to extract a 10 min range (10 min from now until now) extract of a log file. I taught I could simply use a command that would say something like Start=date - 10 min but I didn't find anything. Looks like the only solution would have to code a... (3 Replies)
Discussion started by: Browser_ice
3 Replies
Login or Register to Ask a Question