3

I have a file and a field is a time stamp like 20141028 20:49:49, I want to get the hour 20, so I use the system command :

hour=system("date -d\""$5"\" +'%H'")

the time stamp is the fifth field in my file so I used $5. But when I executed the program I found the command above just output 20 and return 0 so hour is 0 but not 20, so my question is how to get the hour in the time stamp ?

I know a method which use split function two times like this:

split($5, vec, " " )
split(vec[2], vec2, ":")

But this method is a little inefficient and ugly.

so are there any other solutions? Thanks

ningyuwhut
  • 609
  • 12
  • 27
  • possible duplicate of [call a shell command from inside awk and pass some awk variables to the shell command](http://stackoverflow.com/questions/20646819/call-a-shell-command-from-inside-awk-and-pass-some-awk-variables-to-the-shell-co) – The Paramagnetic Croissant Oct 30 '14 at 09:51
  • Specifically, [this answer](https://stackoverflow.com/questions/20646819/call-a-shell-command-from-inside-awk-and-pass-some-awk-variables-to-the-shell-co/20648953#20648953). – The Paramagnetic Croissant Oct 30 '14 at 09:51
  • 1
    @nu11p01n73R my mistake, already corrected Thanks – ningyuwhut Oct 30 '14 at 10:09
  • @TheParamagneticCroissant I read the post you gave. According to the method I should use `last_time_over_budget_in_hour="date -d" " +'%H'" $5 | getline` but it is wrong. The difference is `the awk variable in the post is just after the shell command ` but here the awk variable is in the middle of the shell command` – ningyuwhut Oct 30 '14 at 10:14
  • @nu11p01n73R No, but I just want to know a method in this way – ningyuwhut Oct 30 '14 at 10:16
  • @ningyuwhut i couldnt find any way to read the output from system command. As of the `0` you are getting in `hour` is the exit status of system, being successfull – nu11p01n73R Oct 30 '14 at 10:57

2 Answers2

5

Another way using gawk:

gawk 'match($5, " ([0-9]+):", r){print r[1]}' input_file

If you want to know how to manage externall process output in awk:

awk '{cmd="date -d \""$5"\" +%H";cmd|getline hour;print hour;close(cmd)}' input_file
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
  • +1: Although the answer 'use getline' should probably be made more explicit. – William Pursell Nov 02 '14 at 14:16
  • Thanks, but when I execute this command I got an error ` usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]` – ningyuwhut Nov 02 '14 at 14:51
  • Yes, your are right, I changed a machine and it gets the answer! but can you explain the `cmd="date -d \""$5"\" +%H" `, what's the backslash for? Thanks – ningyuwhut Nov 02 '14 at 15:01
3

You can use the substr function to extract the hour without using system command.

for example:

awk {'print substr("20:49:49",1,2)}'

will produce output as

20

Or more specifically as in question

 $ awk {'print substr("20141028 20:49:49",10,2)}'
 20

substr(str, pos, len) extracts a substring from str at position pos and lenght len

if the value of $5 is 20141028 20:49:49,

$ awk {'print substr($5,10,2)}'
20
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52