Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I compare & get the yesterday date in AWK?

Status
Not open for further replies.

MSosa

Technical User
Jun 9, 2000
1
US
I'm trying to get, based in an input date, lines from an input file.<br><br>I stopped here..<br><br>Today=`date +%Y\/%m\/%d`<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>awk -v HOY=$Today ' { if( substr($1, 4) == HOY ) print $0 }' /SYBASE/install/errorlog<br><br>This return lines that match only with one day; but I need lines that match with a range of dates. Example: <br>Get lines that a field match with dates between today and yesterday.<br>&nbsp;<br><br><br>&nbsp;<br>
 
#!/bin/sh<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File: whatever<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Purpose: Prints lines containing todays or yesterdays date.<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Usage:&nbsp;&nbsp;cat /SYBASE/install/errorlog ¦ whatever<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Notes:&nbsp;&nbsp;Pipe the input file into nawk using the cat command.<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add a re-direct to capture output if desired.<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You must make this shell wrapper file executable<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using chmod +x whatever before running it.<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Change the filname &quot;whatever&quot; to something you like!<br><br><br><br>Today=`date +%Y\/%m\/%d`<br><br>nawk '<br><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;HOY = substr(&quot;'$Today'&quot;, 9)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# expand shell variable<br>&nbsp;&nbsp;&nbsp;&nbsp;HOY = HOY + 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# coerce string to numeric<br><br>&nbsp;&nbsp;&nbsp;&nbsp;yesterday = HOY - 1<br><br>&nbsp;&nbsp;&nbsp;&nbsp;line = substr($1, 9)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# current line ( $0 )<br>&nbsp;&nbsp;&nbsp;&nbsp;line = line + 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# coerce string to numeric<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (( line == HOY ) ¦¦ ( line == yesterday )) print<br>}'<br><br><br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This works on input data like this:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( 09 June 2000 )<br><br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/08 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/09 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/05 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/09 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/08 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/06 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/09 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/07 blah blah blah<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2000/06/08 blah blah blah<br><br>I hope this helps you!<br> <p>flogrr<br><a href=mailto:flogr@yahoo.com>flogr@yahoo.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top