The answer from @Sheel looks good because it finds the first and last comma-separated field (providing that both fields are numbers or strings which contain no comma characters).
Thanks Sheel... But it's possible that I have more that one fields wrapped inside in the last column as well, I need to somehow escape commas inside "". Really appreciate if you can provide an alternative.
I dont think we can do it in a single line. I can suggest you a way though: Filter out the rows having double quotes into a new file and write a command particualry for that file.
---------- Post updated at 07:22 AM ---------- Previous update was at 06:49 AM ----------
Here is another way. Replace the commas withing double quotes with a different character (say pipe) and then read the columns using comma as delimiter.
Command to replace the commas within double quotes
It worked fine for me. See if there are syntactical errors in your file. Here is the script you might be looking for :
Script - read.ksh
# STEP 1 - Replace the commas within double quotes with pipe & write it to a temp file
awk -F'"' 'BEGIN{OFS="\""}{gsub(",","|",$2);print}' inputFile.csv > tempFile.csv
# STEP 2 - Get the first and last columns from temp file
while read line
do
firstColumn=`echo $line|awk -F"," '{print $1}'`
echo "First Column= $firstColumn"
lastColumn=`echo $line|awk -F"," '{print $NF}'`
echo "Last Column= $lastColumn"
# STEP 3 - Replace pipe with comma
finalLastColumn=`echo $lastColumn | sed 's/\|/\,/g'`
echo "Finally Last Column= $finalLastColumn"
done < tempFile.csv
./read.ksh
First Column= "asdf| tew|123"
Last Column= 123456
Finally Last Column= 123456
First Column= abcd
Last Column= 123456
Finally Last Column= 123456
First Column= abcd
Last Column= "asdf| tew|123"
Finally Last Column= "asdf, tew,123"
Missed reverting pipe to comma in first column. You can add these lines after step 3
finalFirstColumn=`echo $firstColumn | sed 's/\|/\,/g'`
echo "Finally first Column= $finalFirstColumn"