0

we want to capture the time from the output and not the time from the command sshpass

expected output

Sun Jul 14 12:47:49 UTC 2019
Sun Jul 14 12:47:49 UTC 2019
Sun Jul 14 12:47:49 UTC 2019

but from the command we get:

sshpass -p customer pssh  -H   "presto01 presto02 presto03"  -l root -A -i "date"  | grep '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
[1] 12:45:45 [SUCCESS] presto01
Sun Jul 14 12:45:45 UTC 2019
[2] 12:45:45 [SUCCESS] presto03
Sun Jul 14 12:45:45 UTC 2019
[3] 12:45:45 [SUCCESS] presto02
Sun Jul 14 12:45:45 UTC 2019

we can do

sshpass -p customer pssh  -H   "presto01 presto02 presto03"  -l root -A -i "date"  | grep '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]' | grep -v "^\["
Sun Jul 14 12:50:24 UTC 2019
Sun Jul 14 12:50:24 UTC 2019
Sun Jul 14 12:50:24 UTC 2019

but this is ugly way

  • What about | grep 'UTC'? – Cyrus Jul 14 at 12:56
  • what in case it will be other word instead UTC? – yael Jul 14 at 12:57
  • Do you really want to grep for the timestamp only - or do you want to exclude the pssh status message? You could do the latter with an inverse match ex. grep -vE '^\[[0-9]+\]' or perhaps just grep -v '^\[' – steeldriver Jul 14 at 13:13
  • about "Do you really want to grep for the timestamp only - or do you want to exclude" , I think both or the best option – yael Jul 14 at 13:15
  • 2
    Well, based on your recent post history, you intend to do arithmetic on these values - in which case I'd suggest using epoch time and grepping for lines that consist entirely of decimal digits: . . . date +%s | grep -xE '[0-9]+' – steeldriver Jul 14 at 14:04
1

Probably the time part of the line is the most stable, if you are concerned about other time zones. Probably then also greping on month and day names would not be safe.

EDIT: made the pattern a bit more precise to not match parts of IP adresses and things like that (the pattern may not match before and after a digit or a colon).

So if you try to grep on the time, you could do:

grep "\([^[^0-9:]\|^\)[0-9]\{1,2\}\(:[0-9]\{2\}\)\{2\}\([^[^0-9:]\|$\)"

With some test data as above:

grep "([^[^0-9:]\|^)[0-9]{1,2}(:[0-9]{2}){2}([^[^0-9:]\|$)" << END Sun Jul 14 12:47:49 UTC 2019 some message the adress was 01:11:91:05 Sun Jul 14 12:47:49 UTC 2019 01:11:91:05 was the address this is no propper time 123:23:03 this also not 1:3:43 and neither his 12:33:27: Sun Jul 14 12:47:49 UTC 2019 END

it outputs:

Sun Jul 14 12:47:49 UTC 2019
Sun Jul 14 12:47:49 UTC 2019
Sun Jul 14 12:47:49 UTC 2019

It requires three numeric parts separated by : the first can be 1-2 digits in length and the other two must be 2 digits in length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.