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