64BIT - The Collection

Bash - Fixing CSVs That Contain Commas In The User-Agent

I have a file that looks like of like a web access log. The last column in the log is a User-Agent, and of course these can have commas in them. So, I needed to write a quick script that would run through the file and replace those commas - I chose to replace it with '_' 

#!/bin/bash


while read line; do

if [[ $line == *"("* ]] #this string has a bracketed useragent!
then
        useragent=`echo $line | cut -d"(" -f 2- | sed 's/,/_/g'`
        firstpiece=`echo $line | cut -d"(" -f 1`
        echo "$firstpiece($useragent" >> fixed-session.csv

else #this string doesn't, so we pass it through!
        echo $line >> fixed-session.csv
fi

done < session.csv