Question: shell script code below # ! / bin / bash # Script to parse logs and generate CSV report # Check if the correct number

shell script code below
#!/bin/bash
# Script to parse logs and generate CSV report
# Check if the correct number of arguments is provided
if ["$#"-ne 3]; then
echo "Usage: ./asgn05.sh "
exit 1
fi
log_file=$1
regex_pattern=$2
json_fields=$3
# Define the fields in the exact order required for CSV output
fields=("action" "additional_info" "data_amount" "location_lat" "user_id")
# Initialize an associative array to hold field values for each line
declare -A field_values
# Function to reset field values to "N/A"
reset_fields(){
for field in "${fields[@]}"; do
field_values["$field"]="N/A"
done
}
# Initialize CSV header
header=$(IFS=,; echo "${fields[*]}")
echo "$header"
# Process each line in the log file
while IFS= read -r line; do
# Reset field values for each new lineue"
reset_fields
if [["$line" =~ ^[a-zA-Z0-9_]+=]]; then
# Pawhile IFS='=' read -r key value; do45)
# Strip whitespace and quotation marks
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs | sed 's/"//g')
# Assign value to appropriate field if it's in the fields array
if [[" ${fields[*]}"==*" $key "*]]; then
field_values["$key"]="$value"
fi
done <<<"$line"
# Parse JSON fields
elif [["$line" =~ ^{.*}$ ]]; then
for json_field in $(echo "$json_fields" | tr ','''); do
# Extract JSON field value using jq
field_name=$(echo "$json_field" | sed 's/\./_/g')
value=$(echo "$line" | jq -r ".$json_field //\"N/A\"")
field_values["$field_name"]="$value"
done
fi
# Output CSV row with values in the correct order
row=""
for field in "${fields[@]}"; do
row+="${field_values[$field]},"
done
echo "${row%,}" # Remove trailing comma
done <"$log_file"
THE OUTPUT I GET
action,additional_info,data_amount,location_lat,user_id
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,12
N/A,N/A,N/A,N/A,N/A
login,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,25
N/A,N/A,N/A,N/A,N/A
purchase,N/A,N/A,N/A,N/A
N/A,Discount applied,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,18
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,42
N/A,N/A,N/A,N/A,N/A
logout,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,15
N/A,N/A,N/A,N/A,N/A
purchase,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,33
N/A,N/A,N/A,N/A,N/A
login,N/A,N/A,N/A,N/A
N/A,N/A,N/A,N/A,N/A
and the output goes on and on, my expected output is
BUT MY INSTRUCTOR SAID L SHOULD BE GETTING THE SAME OUTPUT AS BELOW
action,additional_info,data_amount,location_lat,user_id login,N/A,N/A,40.7128,45 purchase,Promo code applied,200,N/A,58 N/A,N/A,N/A,51.5074,32 logout,N/A,N/A,N/A,100 purchase,N/A,120,N/A,21
sample.log below a few line
---
timestamp=2023-10-01T08:15:30
user_id=12
location={"country":"US","city":"San Francisco","lat":37.7749,"long":-122.4194}
action=login
---
timestamp=2023-10-01T09:30:22
user_id=25
data={"value":20,"type":"purchase","amount":500}
action=purchase
additional_info=Discount applied
---
timestamp=2023-10-01T11:05:50
user_id=18
data={"value":10,"type":"login"}
---
timestamp=2023-10-02T07:22:11
user_id=42
location={"country":"US","city":"Los Angeles","lat":34.0522,"long":-118.2437}
action=logout
status=failed
---
timestamp=2023-10-02T13:47:10

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!