extracting part of a text file

Hi guys

So I have a very large log file where each event is logged along with the time that it occurred.

So for e.g. The contents of the file look like:

...
12:00:07 event 0 happened.
12:01:01 event 1 happened.
12:01:05 event 2 happened.
12:01:30 event 3 happened.
12:02:01 event 4 happened.
12:02:40 event 5 happened.
12:03:40 event 6 happened.
12:04:06 event 6 happened.
...

I want to extract every thing in that file listed between a given start time and an end time.

so some thing like all events between
the first entry of 12:01:00 and the last entry of 12:03:59
and store them in a random text file. Note time stamps can be repeated and therefore I mentioned the first and the last entries.

And not all lines in the log file start with a time stamp. Some lines are just random information about the server etc. But I want to extract those lines as well.

Hope I have clarified my question.

And thanks in advance every one :slight_smile:

ali

Use CODE tags when posting code, data or logs for better readability and to preserve formatting like indention etc., ty.

Besides adding code tags moving it to shell scripting area - nothing AIX particular in here.

You can start from this example:

awk '{a="file."srand()}/12:01/,/12:03/{print >> a}'  file
sed  -n '/^12:00/,/^12:03/p' event.log > new_event.log

Or a nice simple grep would do it:

grep -E "12:01|12:02|12:03" <input_text_file_name> > <output_text_file_name>

So here we use the grep command to search for multiple "arguments", delimited by the pipe (|) symbol in the <input_text_file_name> and then redirect with the > to the <output_text_file_name> and then you should have the results you want in the <output_text_file_name> file.

pSeries and AIX Information Center

HTH

This may not meet all the requirements.
Remember the requirement says:
"""And not all lines in the log file start with a time stamp. Some lines are just random information about the server etc. But I want to extract those lines as well."""

yeah it wouldn't and this is more simpler

grep 12:0[1-3] event.log

hi guys
Thanks for your replies so far

This pattern doesnt do anything :
sed -n '/^12:00/,/^12:03/p' event.log > new_event.log

And the grep ones I believe will only extract lines that contain the pattern that I have entered. But they would miss the lines in between. I do need the lines without the pattern as well.

I again summarise my requirement:
In a file extract every thing from the first occurance of pattern: 12:01:00 till the last occurance of pattern: 12:03:00.

many thanks
ali

I have no issues.
Is it possible your input file is DOS formatted?

Here is my working.

cat event.log
12:00:07 event 0 happened.
some line at 2
12:01:01 event 1 happened.
some line at 4
12:01:05 event 2 happened.
12:01:30 event 3 happened.
some line at 7
some line at 8
12:02:01 event 4 happened.
12:02:40 event 5 happened.
12:03:40 event 6 happened.
12:04:06 event 6 happened.

Command:

sed -n '/^12:00/,/^12:03/p' event.log

Output:

12:00:07 event 0 happened.
some line at 2
12:01:01 event 1 happened.
some line at 4
12:01:05 event 2 happened.
12:01:30 event 3 happened.
some line at 7
some line at 8
12:02:01 event 4 happened.
12:02:40 event 5 happened.
12:03:40 event 6 happened.

May be version issues?
Remove the "> new_event.log" part and check.

Did you ever try my previous solution

Try with a gap before the "p" like this.

sed -n '/^12:00/,/^12:03/ p' event.log > new_event.log