0

I'm accessing a log file where I have the timestamps at the beginning of each log. So I'm extracting the time and storing it in a variable to later convert it to epoch time.

So the problem that I'm facing is that whenever I execute it, It says date: invalid date and print the time stamps with a \n next to it. example, 10/23/19 15:45:01\n10/23/19 15:45:11. Also this output comes up in between when I'm printing the normal timestamps (this issue stops everytime I comment the date function)

I've tried doing it something like this:

error_time=$(
    cat file | 
    grep -i 'word' -A 5 | 
    grep -o '[0-9][0-9]/[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
)
epoch_time=$(
    date -d '$error_time' +%s
) or $(
    date --date'$error_time' +%s(also +'%s')
)

for error_dir in $(ls -d $path)
do
    for error_logs in $(ls $error_dir)
    do
        error_time=$(
            cat $error_dir/$error_logs | 
            grep -i 'word' -A 5 | 
            grep -o '[0-9][0-9]/[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
        )
        epoch_time=$(
            date -d '$error_time' +%s
        ) or $(
            date --date'$error_time' +%s(also +'%s')
        )
    print $epoch_time
    done
done

the expected output should be in epoch time(seconds as far as I know) and what I'm getting is like this \n09/26/19 14:13:37\n09/26/19 14:34:31\n09/26/19 15:22:01.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • 1
    What does the contents of your `file` look like? Can you reformat your question to show the blocks of code you are invoking, by indenting your script with 4 leading space characters/ – Mark Stewart Nov 04 '19 at 20:19
  • 1
    Don't use `ls` for iterating over filenames. – Mika Feiler Nov 04 '19 at 20:44
  • 1
    i think `'$error_time'` might evaluate to just `'$error_time'` unlike `"$error_time"` which would evaluate to the contents of `$error_time` variable. – Mika Feiler Nov 04 '19 at 20:49
  • always double-quote the uses of parameter expansion unless you want them to end up causing argument separation – Mika Feiler Nov 04 '19 at 20:50
  • 1
    What is this `or` command you're calling? What shell is this? bash does not have a builtin `print` command but ksh does. – glenn jackman Nov 04 '19 at 21:02
  • 1
    Following up on "don't use `ls`" -- https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29 – glenn jackman Nov 04 '19 at 21:06
  • @glennjackman you might be right, i don't know of any `or` command in bash — so maybe it's some zsh or something. Idk if bash would treat it as a syntax error or incorporate it as a string… – Mika Feiler Nov 04 '19 at 22:17
  • Bash will treat it like `var=value cmd arg ...` which sets the variable only for the duration of the command. But now I see that the OP is writing a kind of pseudocode – glenn jackman Nov 04 '19 at 22:33

2 Answers2

1

perl is really well suited to this type of problem:

#!/usr/bin/env perl

# Replace occurrences of 'mm/dd/yy HH:MM:SS' with epoch time

use strict;
use warnings;
use Time::Piece;

while(<>) {
        s@(\d{1,2}/\d{1,2}/\d\d \d{1,2}:\d\d:\d\d)@
                sprintf scalar Time::Piece->strptime(
                $1, "%m/%d/%y %H:%M:%S")->epoch@ge;
        print;
}

This gives results like:

$ printf '1/3/19 4:23:04 text \ntext: 12/14/19 12:35:23\n' | ./a.pl
1546489384 text 
text: 1576326923
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

You script after fixing could look like this:

# don't parse ls
for f in "$path"/*/*; do
    #    ^     ^ quote your variables
    error_time=$(
        # don't try to win useless cat award
        grep -i 'word' -A 5 "$f" | 
        #                   ^  ^ quote your variables
        grep -o '[0-9][0-9]/[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
    )
    epoch_time=$(date -d"$error_time" +%s)
    #                   ^           ^ note
    printf "%d\n" "$epoch_time"
    #             ^           ^ quote your variables
    #    ^ there is no "print" command     
done

It could be wise to get to know:

KamilCuk
  • 120,984
  • 8
  • 59
  • 111