awk removing data before or after a pattern

I have the following data:

01:00:00 29 10 20 41
01:20:00 18 6 34 42
01:40:00 28 5 24 43
02:00:01 11 7 8 74
02:20:01 19 15 12 54
02:40:01 1 4 0 95
03:00:01 1 3 0 96
03:20:01 0 0 0 99
03:40:01 1 2 0 97

First I want to remove all data after a certain pattern, Ie 03:00 sp this
should leave me with the following results

01:00:00 29 10 20 41
01:20:00 18 6 34 42
01:40:00 28 5 24 43
02:00:01 11 7 8 74
02:20:01 19 15 12 54
02:40:01 1 4 0 95
03:00:01 1 3 0 96

Secondly I would also like to know how to remove all before the pattern
so this should leave me with the following results.

03:00:01 1 3 0 96
03:20:01 0 0 0 99
03:40:01 1 2 0 97

Note: Both these statements not be run at the same time.

Thanks to all who answer.

Removing after pattern:

awk '!p;/^03:00/{p=1}' file

Removing before pattern:

awk '/^03:00/{p=1}p' file

This is one way to accomplish what you need; may not be the most efficient, but is easy to understand. It will do both 'drop before' and 'drop after' functions. I've set it up to match strings, not patterns, if you truly need to match patterns, use the match() function in awk rather than index().

#!/usr/bin/env ksh

# parms:        $1 -- before string; all records before matching this string are dropped
#                       If this is "none" then all records until the after string is matched are kept
#               $2 -- after string; all records after this string are dropped.

awk -v toss_before="${1:-none}" -v toss_after="$2" '
        BEGIN {
                if( toss_before == "none" )     # keep everything from the start
                        snarf = 1;
                else
                        snarf = 0;              # must wait until we see toss_before to start keeping data
        }

        {
                if( snarf ) 
                {
                        printf( "%s\n", $0 );           # print if snarfing 

                        if( toss_after && index( $0, toss_after ) )     # check to see if this has the end string
                                exit( 0 );
                }
                else                                    # not snarfing, see if this is the start string
                {
                        if( index( $0, toss_before ) )
                        {
                                printf( "%s\n", $0 );
                                snarf = 1;
                        }
                }
        }
'

Could you pls explain why it is working?? its seems interesting. I knw the basis of awk but how this printing is happening by assigning one variable ? is it due to some special variable like $_ in perl??

His script is taking advantage of the fact that if the action part of a pattern-action pair is missing, the action {print;} is assumed. Further, !p is the same as saying 'p == 0' in the pattern portion of the statement. The value of an undefined variable is either 0 or "" depending on how it is used. To write the script in 'long form' might make more sense to you:

awk ' 
BEGIN { p = 0;}
p == 0 { print; }       # pattern not matched; print  (!p;)
/^03:00/ { p = 1; }  # pattern is matched, stop printing (exit(0) would be more efficient)
' file

Similarly, printing everything after the pattern line:

BEGIN { p = 0;}
/^03:00/ { p = 1; }  # pattern is matched, allow printing
p != 0 { print; }       # p;  in the original script
' file
1 Like
sed -n '1,/03:00/p' file
sed -n '/03:00/,$p' file
awk '$1<"03:00:00"' urfile
awk '$1>="03:00:00"' urfile