Replacing date with EPOCH in column output
print
appends a newline by default. Use printf
:
awk '{printf "%s %s ",$1,$2; system("date -d "$3" +%s")}' data
If a special output field separator OFS
had been defined, you could use
awk '{printf "%s%s%s%s",$1,OFS,$2,OFS; system("date -d "$3" +%s")}' data
Notice that each %s
gets one of the remaining arguments to printf
.
Output:
user1 1234 1577847600
user2 2345 1580612400
If you have GNU awk (aka gawk
) you could use its built-in time mktime
function (although the input date format needs some manipulation first):
$ gawk 'split($3,a,"/") {$3 = mktime(sprintf("%04d %02d %02d 00 00 00",a[3],a[2],a[1]))} 1' data
user1 1234 1577854800
user2 2345 1580619600
Alternatively, Miller provides a C-style strptime
and strftime
:
$ mlr --nidx put '$3 = strftime(strptime($3,"%d/%m/%Y"),"%s")' data
user1 1234 1577854800
user2 2345 1580619600
or similarly in Perl, with the Time::Piece module:
$ perl -MTime::Piece -alne '
$t = pop @F; print join " ", @F, Time::Piece->strptime($t,"%d/%m/%Y")->strftime("%s")
' data
user1 1234 1577854800
user2 2345 1580619600