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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare date header using awk/sed command and update log file? 1

Status
Not open for further replies.

rchowd2

Technical User
Jan 4, 2005
8
US
I am trying to compare two similar files which contains differnt header date information. If header dates are different from each other I would like to update the header information to the log file. Any advise how how can I go by and update the log file.

file1 header:
HEADER 2/16/0515.03.30TIO01676 CMS.TST.IWUNV676.IO01 $

file2 header:
HEADER 2/18/0515.03.30TIO01676 CMS.TST.IWUNV676.IO01 $

If file2header (example: 2/18/05) > file 1 (2/16/05)

print (file 2 header info) >> log.file

End of Program!


Thanks in advance for your help on this matter.

Russel

 
Code:
awk '{ if (! Date[1]) { split($1, Date, "[/.T]"); nextfile }
    split($1, Tmp, "[/.T]"); i = 0
    while (Tmp[++i]) if (Tmp[i] > Date[i]) print #> "log.file"
    nextfile
}' file1 file2 >>log.file

Or use $2 if the header contains 'HEADER'. Alternativley you could go like awk 'NR == 1 { .code. }' without doing nextfile.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
(no real date comparision, just the numbers)
 
Change to $2 again if wanted.

Code:
gawk '{ date = substr($1, match($1, ".*/.."), RLENGTH)
  split(date, Date, "/")
  if (Date[3] ~ /^0/) Date[3] = 20 Date[3]
  else Date[3] = 19 Date[3]
  time=substr($1, RLENGTH+1); gsub(/\./, " ", time)

  newdate = Date[3]" "Date[1]" "Date[2]" "time":"
  secs = mktime(newdate)

  if (! base_time) base_time = secs
  else { if (secs > base_time) print }
} NR>1 { nextfile }' file1 file2
 
Thank you so much "XMB"! I really appreciate your help.
 
Have fun, but ehm.. this is the correct version with right FS:
Code:
gawk -F'[ \tT]+' -v f=2 'FNR==1 {
  date = substr($f, match($f, ".*/.."), RLENGTH)
  split(date, Date, "/")
  if (Date[3] ~ /^0/) Date[3] = 20 Date[3]
  else Date[3] = 19 Date[3]

  time = substr($f, RLENGTH + 1); gsub(/\./, " ", time)
  newdate = Date[3]" "Date[1]" "Date[2]" "time":"
  secs = mktime(newdate)

  if (! base_time) base_secs = secs
  else if (secs > base_secs) { base_secs = secs; print }
  nextfile
}' file1 file2

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
That may not work either.. Here's a specialized version, have fun =)
Code:
gawk '{ date = gensub(".* ?([0-9]+)/([0-9]+)/([0-9][0-9])([0-9]+)\\.([0-9]+)\\.([0-9][0-9]).*", "\\3 \\1 \\2 \\4 \\5 \\6:", 1); if (date ~ /^[0-3]/) date = 20 date; else date = 19 date; cmpN(mktime(date)) } function cmpN(n) { if (! base_N) base_N = n; else if (n > base_N) print }' f1 f2
---
{ date = gensub(
    ".* ?([0-9]+)/([0-9]+)/([0-9][0-9])([0-9]+)\\.([0-9]+)\\.([0-9][0-9]).*",
    "\\3 \\1 \\2 \\4 \\5 \\6:", 1)
   if (date ~ /^[0-3]/) date = 20 date; else date = 19 date
   cmpN(mktime(date))
}
function cmpN(n) { if (! base_N) base_N = n; else if (n > base_N) print }

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Thanks XMB! But I do have one more question for you. If file 2 doesn't exist, how can I just simply update the log file without going through any date comparison.

Example:

file1 header:
HEADER 2/16/0515.03.30TIO01676 CMS.TST.IWUNV676.IO01 $


We just want to update the log file with the following info:

HEADER 2/16/0515.03.30TIO01676 CMS.TST.IWUNV676.IO01


Thanks again,

Russel
 
function cmpN(n) { if (! base_N) base_N = n; else if (n > base_N) { printed++; print } }
END { if (! printed) print base_N }


Cheers.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top