Published on http://www.itswapshop.com (http://www.itswapshop.com)

Home > How to Remove Commas Inside of Quotes From a CSV or Text File on Linux with Awk

How to Remove Commas Inside of Quotes From a CSV or Text File on Linux with Awk

Submitted by ingram on Wed, 07/22/2015 - 2:22pm
How to Remove Commas Inside of Quotes From a CSV or Text File on Linux with Awk

Let's say you have a CSV file that you would like to open in Excel or LibreOffice Calc to analzye. You find out the formatting is broken because there is a column that has quoted text with a comma in it:

Name, Country, City

Jason, US, Memphis

David, US, Little Rock

"Karam, Sage", US, Nazareth

As you can see, this may cause a problem when importing the file into your spreadsheet application as the 4th row has 4 commas, and the others only have 3.

This is easy to fix using Awk. Simply run this one-liner command, specifying your source file, and then a new file to output it to:

awk -F'"' -v OFS='' '{ for (i=2; i<=NF; i+=2) gsub(",", "", $i) } 1' source_file.csv >processed_file.csv

Privacy Policy

support @ itswapshop . com


Source URL: http://www.itswapshop.com/tutorial/how-remove-commas-inside-quotes-csv-or-text-file-linux-awk