Question: shell script code below # ! / bin / bash # Script to parse logs and generate CSV report # Check if the correct number
shell script code below
#binbash
# Script to parse logs and generate CSV report
# Check if the correct number of arguments is provided
if $#ne ; then
echo "Usage: asgnsh
exit
fi
logfile$
regexpattern$
jsonfields$
# Define the fields in the exact order required for CSV output
fieldsaction "additionalinfo" "dataamount" "locationlat" "userid
# Initialize an associative array to hold field values for each line
declare A fieldvalues
# Function to reset field values to NA
resetfields
for field in $fields@; do
fieldvalues$field"NA
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"
resetfields
if $line" ~ azAZ; then
# Pawhile IFS read r key value; do
# Strip whitespace and quotation marks
key$echo $key" xargs
value$echo $value" xargs sed sg
# Assign value to appropriate field if it's in the fields array
if $fields $key ; then
fieldvalues$key"$value"
fi
done $line"
# Parse JSON fields
elif $line" ~ $ ; then
for jsonfield in $echo $jsonfields" tr ; do
# Extract JSON field value using jq
fieldname$echo $jsonfield" sed sg
value$echo $line" jq r $jsonfield NA
fieldvalues$fieldname"$value"
done
fi
# Output CSV row with values in the correct order
row
for field in $fields@; do
row$fieldvalues$field
done
echo $row # Remove trailing comma
done $logfile"
THE OUTPUT I GET
action,additionalinfo,dataamount,locationlat,userid
NANANANANA
NANANANANA
NANANANA
NANANANANA
loginNANANANA
NANANANANA
NANANANANA
NANANANA
NANANANANA
purchase,NANANANA
NADiscount applied,NANANA
NANANANANA
NANANANANA
NANANANA
NANANANANA
NANANANANA
NANANANANA
NANANANA
NANANANANA
logoutNANANANA
NANANANANA
NANANANANA
NANANANANA
NANANANA
NANANANANA
purchase,NANANANA
NANANANANA
NANANANANA
NANANANA
NANANANANA
loginNANANANA
NANANANANA
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,additionalinfo,dataamount,locationlat,userid loginNANA purchase,Promo code applied,NA NANANA logoutNANANA purchase,NANA
sample.log below a few line
timestampT::
userid
locationcountry:US"city":"San Francisco","lat":"long":
actionlogin
timestampT::
userid
datavalue:"type":"purchase","amount":
actionpurchase
additionalinfoDiscount applied
timestampT::
userid
datavalue:"type":"login
timestampT::
userid
locationcountry:US"city":"Los Angeles","lat":"long":
actionlogout
statusfailed
timestampT::
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
