0

I have an output from a shell script like this:

aaa.sh output

 Tue Mar 04 01:00:53 2014 Time drift detected. Please check VKTM trace file for more details. Tue Mar 04 07:21:52 2014 Time drift detected. Please check VKTM trace file for more details. Tue Mar 04 13:17:16 2014 Time drift detected. Please check VKTM trace file for more details. Tue Mar 04 16:56:01 2014 SQL> ALTER DISKGROUP fra ADD DISK '/dev/rhdisk20' Wed Mar 05 00:03:42 2014 Time drift detected. Please check VKTM trace file for more details. Wed Mar 05 04:13:39 2014 Time drift detected. Please check VKTM trace file for more details. Tue Mar 05 05:56:07 2014 GMON querying group 3 at 10 for pid 18, osid 27590856 GMON querying group 3 at 11 for pid 18, osid 27590856 

I need to get the part, beginning from today's date:

 Wed Mar 05 00:03:42 2014 Time drift detected. Please check VKTM trace file for more details. Wed Mar 05 04:13:39 2014 Time drift detected. Please check VKTM trace file for more details. Tue Mar 05 05:56:07 2014 GMON querying group 3 at 10 for pid 18, osid 27590856 GMON querying group 3 at 11 for pid 18, osid 27590856 
3
  • I'm not a professional on bash. So i need a clear solution. Commented Mar 5, 2014 at 8:58
  • 1
    @AloneInTheDark man awk or man sed has all the information you need to find a solution. Also man date. If you still can't manage to do it out you should add your attempt to the question. Commented Mar 5, 2014 at 9:01
  • 1
    The solution is to become a little professional on bash. That does need several hours of reading Bash Guide for Beginners, Advanced Bash Scripting Guide, gawk manual Commented Mar 5, 2014 at 9:15

5 Answers 5

3

You can get the date in the correct format like this:

today=$(date +'%a %b %d') 

and then search for it like this:

grep "$today" aaa.sh 

If there are lines from today without a date, such as your GMON lines, you could add -A to say how many lines after the match you want and use a big number:

grep -A 999999 "$today" aaa.sh 

If you are on AIX and there is no -A option, use sed like this:

today=$(date +'%a %b %d') sed -n "/${today}/,$ p" aaa.sh 

Explanation:

That says store today's date in the variable today in the format "Wed Mar 05". Then search, without printing anything (-n) till you find that date, From that point on, till the end of file ($) print all lines (p).

Sign up to request clarification or add additional context in comments.

3 Comments

How to do that in AIX? Because there is no -A parameter in that.
I got an error: aaa.sh[10]: Wed: not found. sed: 0602-410 The first regular expression cannot be null. My date format is like that("Wed Mar 05")
Try again, I added braces.
1

I think I have an easy solution:

Get date to output the date in a format that would match the date in the file (check man date on formatting options). Since we don't want to match the hours/minutes/seconds we have to call date twice: once for the weekday/month/day half and once for the year half on the end of the full date. Between these two halves we match the horus/minutes/seconds with .* regex.

Then do:

aaa.sh | grep -E '`date --only-weekday-month-day`.*`date --only-year`' -A 999999 

Comments

1

though I am using answer by NewWorld it can be modified as,

convert output of date similar to your file format

suppose in variable 'D'you get that output

sed '1,/${D}/d' aaa.sh 

that will output all lines after match date match.

example: suppose you get D="Wed Mar 05 00:03:42 2014"

output will be as expected.

3 Comments

Thanks for the solution. I have another question, how to do the same thing in AIX? Because syntax is different.
@AloneInTheDark the above solution prints all lines after the pattern match, it excludes the first line. Just FYI
You can use sed -n "/${D}/,$ p" aaa.sh to avoid that.
1

You can use

 tail -n 7 filename 

for getting the desired output . It will basically give you the last seven lines of the text file named filename .

For getting solution from today's date you can use :

 k=$(date +"%a %b %d") g=$(grep -nr "$k" in|cut -f1 -d:|head -1) total=$(wc -l<in) l=`expr $total - $g + 1 tail -n$l in 

1 Comment

I need the part from beginning of today's date. There may be much more lines.
1

Try

sed -n '/Wed Mar 05/,$p' aaa.sh

Here -n means "don't print anything unless specified to". First appearance of a line that matches the expression /Wed\ Mar\ 05/ till the end of the file, will be printed(p)"

1 Comment

Just a friendly word of advice... you do not need to escape the spaces with slashes. And you do not need to specify -e when there is only one piece of code to execute since sed knows what it is and what to do with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.