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!

awk, convering to unixtime 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

hello,
I have a log file in which first + last line are:

2015/11/26 10:12:39 [27741] building file list
2015/11/26 10:17:31 [27741] total size is 33199684413 speedup is 5914.76

I want to check how log rsync was running by subtracting dates from line 2 to 1 (converted to epoch).

my current solution is:
# sed -e 1b -e '$!d' logfile|awk '{print $1,$2}'|sed 'N;s/\n/ /'|while read s0 s1 e0 e1;do echo $(date -d "$e0 $e1" "+%s")-$(date -d "$s0 $s1" "+%s")|bc;done
292

can one awk command do it without extra commands and pipes?
 
Hi

Well, one GNU Awk command certainly can :
Code:
[blue]master #[/blue] awk '[teal]{[/teal][navy]e[/navy][teal]=[/teal][COLOR=orange]mktime[/color][teal]([/teal][COLOR=orange]gensub[/color][teal]([/teal][fuchsia]/[/:]/[/fuchsia][teal],[/teal][i][green]" "[/green][/i][teal],[/teal][i][green]"g"[/green][/i][teal],[/teal][navy]$1[/navy][i][green]" "[/green][/i][navy]$2[/navy][teal]))}[/teal][navy]NR[/navy][teal]==[/teal][purple]1[/purple][teal]{[/teal][navy]s[/navy][teal]=[/teal]e[teal]}[/teal][b]END[/b][teal]{[/teal][b]print[/b] e-s[teal]}[/teal]' logfile
292

Other Awk implementations which have no [tt]mktime()[/tt] function would need to calculate themselves. So I would keep calling the external [tt]date[/tt] command, just without the piping :
Code:
[blue]master #[/blue] awk '[teal]{[/teal][navy]e[/navy][teal]=[/teal][navy]$1[/navy][i][green]" "[/green][/i][navy]$2[/navy][teal]}[/teal][navy]NR[/navy][teal]==[/teal][purple]1[/purple][teal]{[/teal][navy]s[/navy][teal]=[/teal]e[teal]}[/teal][b]END[/b][teal]{[/teal][b]print[/b] [COLOR=orange]t[/color][teal]([/teal]e[teal])[/teal]-[COLOR=orange]t[/color][teal]([/teal]s[teal])}[/teal][COLOR=orange]function t[/color][teal]([/teal]d[teal]){([/teal][i][green]"date -d [/green][/i][lime]\"[/lime][i][green]"[/green][/i]d[i][green]"[/green][/i][lime]\"[/lime][i] [/i][lime]\"[/lime][i][green]+%s[/green][/i][lime]\"[/lime][i][green]"[/green][/i][teal])|[/teal][b]getline[/b] d[teal];[/teal][b]return[/b] d[teal]}[/teal]' logfile
292


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top