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