Below is the scenario:
Given a file that contains a log (timestamp, customer id, page id), please write a script to parse it and output the list of pages visited by each customer.
Input CSV File:
Time, Customer ID, Page ID 1, C1, P1 2, C2, P2 3, C3, P3 4, C2, P1 5, C2, P3 6, C2, P2 7, C1, P3 8, C1, P2 9, C3, P1 10, C2, P1 11, C2, P3 12, C2, P2 13, C1, P1 14, C1, P3 15, C1, P2Example execution of script. The Customer ID must be passed as a parameter. That is,
./script "C1"Output:
P1, P3, P2, P1, P3, P2
As of now, I got the following code to parse a CSV file
Code:
INPUT=/filepath/customers.csv
CUSTOMER_NAME=$1
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read f1 f2 f3
do
echo "Time : $f1"
echo "Customer ID : $f2"
echo "Page_ID : $f3"
done < $INPUT
IFS=$OLDIFS
How can I write the logic to filter the data based on the customer input?
awk -F', ' -vq=C1 '$2==q{printf "%s%s",sep,$3;sep=FS} END{print ""}' file, or keep pages in an array and join them in the END block