-1

I am trying to add a status column in the last cell based on timestamp column in unix. I am not sure how to proceed.

Desired I/p:

Date Location 
2023-02-08 02:19 /tmp/SA
2023-02-07 01:24 /tmp/SA2

Expected Output:

Date Location  Status
2023-02-08 02:19 /tmp/SA  Success
2023-02-07 01:24 /tmp/SA2  Failure

I was trying the below solution but got blank result:

awk '{ if ($1==date) print success; else $1!=date; print failure; }' file.txt
Dominique
  • 16,450
  • 15
  • 56
  • 112

4 Answers4

0

Since your date has a fixed length, an easy way to extract it is using Parameter Expansion (see this answer).

So you iterate all lines from input, extract the date and simply compare with the desired date:

$ cat check_date.sh
#!/bin/bash

while read line; do

    date=${line:0:16}

    if [ "${date}" = "$1" ]; then
        echo "${line} Success"
    else
        echo "${line} Failure"
    fi  

done < input.txt

Here's a sample run:

$ cat input.txt
2023-02-08 02:19 /tmp/SA
2023-02-07 01:24 /tmp/SA2

$ ./check_date.sh "2023-02-08 02:19"
2023-02-08 02:19 /tmp/SA Success
2023-02-07 01:24 /tmp/SA2 Failure
Jardel Lucca
  • 1,115
  • 3
  • 10
  • 19
0

It looks like you need to explain how to make awk understand what a date looks like. I have the impression that your date format satisfies following regular expression:

[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]

I believe that such checking is done with the ~ operator in awk.

Dominique
  • 16,450
  • 15
  • 56
  • 112
0

Getting the date in the desired format (%Y-%m-%d) may vary depending on your OS (BSD, GNU, etc)

A BSD date version might look like this

% awk -v date="$(date +%Y-%m-%d)" -v OFS="\t" '
    NR>1{if(date == $1){print $0,"Success"} else {print $0,"Failure"}}' file
2023-02-08 02:19 /tmp/SA    Success
2023-02-07 01:24 /tmp/SA2   Failure

Data

% cat file
Date Location
2023-02-08 02:19 /tmp/SA
2023-02-07 01:24 /tmp/SA2
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
0

Alternative awk solution:

target_ts="2023-02-08 02:19" Set target_ts variable.

'NR==1{$3="Status"} Add 'Status' header column to header record.

NR>1&&d==$1" "$2{$4="Success"} For data records add 4th column containing 'Success' if timestamp matches target_ts.

NR>1&&d!=$1" "$2{$4="Failure"} For data records add 4th column containing 'Failure' if timestamp does not match target_ts.

Use awk idiomatic '1' to print.


Code:

target_ts="2023-02-08 02:19"

awk -v d="$target_ts" '
NR==1{$3="Status"}
NR>1&&d==$1" "$2{$4="Success"}
NR>1&&d!=$1" "$2{$4="Failure"}1' file.dat

Output:

Date Location Status
2023-02-08 02:19 /tmp/SA Success
2023-02-07 01:24 /tmp/SA2 Failure


BASH alternative:

#!/bin/bash

target_date="$1"
data_file="$2"

{
    read -r header ; printf "%s Status\n" "$header"
    while read -r line ; do
        case "${line:0:16}" in
            "${target_date}") status="Success" ;;
            *) status="Failure" ;;
        esac
        printf "%s %s\n" "$line" "$status"
    done
} < "$data_file"

Script Execution: (pass timestamp as 1st param and file path as 2nd param)

./script "2023-02-08 02:19" file.dat

Output:

Date Location Status
2023-02-08 02:19 /tmp/SA Success
2023-02-07 01:24 /tmp/SA2 Failure
j_b
  • 1,975
  • 3
  • 8
  • 14