I am using Sun OS 5.10
I am Using nawk to extract specific column from csv file.
The third column of csv is the time in Milliseconds and I need to convert it to Date then save it in another csv file.
I am use this command to extract the columns I need and save it in tttn.csv
nawk 'BEGIN {FS = ","}; NR>1 split($42,a,"|") {print $3 "," $46 "," $48 ","
$47 "," null "," null "," a[2] "," $6 "," $49 "," $7 "," $8 ","}' ttt.csv >
tttn.csv
What I should write to convert the third column $3 from milliseconds to Date
Here is on sample from the third column 1519208012.638884910
and I need to convert it to be in this format '02/21/2018 17:04:95'
How can I handle this task?
rdrtx1
March 26, 2018, 3:33pm
2
print strftime("%D %T", $3)
dt="date +\"%D %T\" -d @" $3
dt | getline $3
close (dt)
print $3
strftime
is a GNU extension - Solaris' nawk
is not gawk
.
---------- Post updated at 04:49 PM ---------- Previous update was at 04:15 PM ----------
Here's the old Solaris hack - epoch->readable - don't have the OS right now to validate - YMMV:
currentEpoch='1519208012'
echo "0t${currentEpoch}=Y" | /usr/bin/adb
---------------------
Where should i write this commands
---------- Post updated at 05:03 PM ---------- Previous update was at 05:02 PM ----------
vgersh99:
strftime
is a GNU extension - Solaris' nawk
is not gawk
.
---------- Post updated at 04:49 PM ---------- Previous update was at 04:15 PM ----------
Here's the old Solaris hack - epoch->readable - don't have the OS right now to validate - YMMV:
currentEpoch='1519208012'
echo "0t${currentEpoch}=Y" | /usr/bin/adb
-----------------
Where Should I write this command
You can try /usr/bin/adb like vgersh99 suggests (I'm not sure what format your date will appear in however) like this:
awk 'BEGIN {FS = ","}; NR>1 split($42,a,"|") {
TM=$3
sub(/[.].*$/,x,TM)
CMD="echo \"0t\" TM | /usr/bin/adb"
CMD | getline tm
close (CMD)
print tm "," $46 "," $48 "," $47 "," null "," null "," a[2] "," $6 "," $49 "," $7 "," $8 ","}' ttt.csv > tttn.csv
or use perl like this:
awk 'BEGIN {FS = ","}; NR>1 split($42,a,"|") {
TM=$3
sub(/[.].*$/,x,TM)
CMD="perl -we '\''use POSIX ; print strftime(\"%m/%d/%Y %T\", localtime(" TM "))'\''"
CMD | getline tm
close (CMD)
print tm "," $46 "," $48 "," $47 "," null "," null "," a[2] "," $6 "," $49 "," $7 "," $8 ","}' ttt.csv > tttn.csv
chubler_xl:
You can try /usr/bin/adb like vgersh99 suggests (I'm not sure what format your date will appear in however) like this:
awk 'BEGIN {FS = ","}; NR>1 split($42,a,"|") {
TM=$3
sub(/[.].*$/,x,TM)
CMD="echo \"0t\" TM | /usr/bin/adb"
CMD | getline tm
close (CMD)
print tm "," $46 "," $48 "," $47 "," null "," null "," a[2] "," $6 "," $49 "," $7 "," $8 ","}' ttt.csv > tttn.csv
or use perl like this:
awk 'BEGIN {FS = ","}; NR>1 split($42,a,"|") {
TM=$3
sub(/[.].*$/,x,TM)
CMD="perl -we '\''use POSIX ; print strftime(\"%m/%d/%Y %T\", localtime(" TM "))'\''"
CMD | getline tm
close (CMD)
print tm "," $46 "," $48 "," $47 "," null "," null "," a[2] "," $6 "," $49 "," $7 "," $8 ","}' ttt.csv > tttn.csv
thanks for your reply but the first solution give the following error :
adb: syntax error on line 1 of (stdin) near "tm"
the second solution works but with very low performance
Could you please help me to enhance the performance or fix the fist solution error
note that I am using nawk because awk and gawk gives me errors too.
Oh so you have gawk on your system then.
Give this a go:
gawk 'BEGIN {FS = ","}; NR>1 split($42,a,"|") {
print strftime("%D %T", $3) "," $46 "," $48 "," $47 "," null "," null "," a[2] "," $6 "," $49 "," $7 "," $8 ","}' ttt.csv > tttn.csv