Issue
I'm new to bash scripting and want to extract code and uid (unique) from log file into new csv file for further processing.
The file contains:
2021-02-16T10:13:54.629Z,app,"Code: 01-87-04 & Diff [{:uid=>""asdf1234"", :sid=>636614}] / [{:uid=>""asdf1234"", :sid=>0}]"
2021-02-16T10:11:22.914Z,app,"Code: 01-33-05 & Diff: [{:uid=>""kx4oa3hv"", :sid=>644448}, {:uid=>""v7jfxsum"", :sid=>643063}] / [{:uid=>""kx4oa3hv"", :sid=>0}, {:uid=>""v7jfxsum"", :sid=>0}]"
The desired output:
01-87-04, asdf1234
01-33-05, kx4oa3hv
01-33-05, v7jfxsum
I started with this script and blocked on extracting the multiple uids. Any help is appreciated.
#!/bin/bash
echo "----->Loading file"
filename="some.csv"
while read -r line; do
scene_id=`echo "$line" | awk '{print $2}'`
echo $scene_id
done < "$filename"
Solution
Using any awk and sort in any shell on every Unix box:
$ awk -v OFS=', ' '{n=split($0,uids,/""/); for (i=2;i<n;i+=2) print $2, uids[i]}' file | sort -u
01-33-05, kx4oa3hv
01-33-05, v7jfxsum
01-87-04, asdf1234
Answered By - Ed Morton