Bash - How to parse a CSV (Or properties files)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

How to parse a CSV or property (ini) in bash

3 - Snippet

3.1 - Ini

DIR_PATH="."
FILE_PATTERN="*.ini"
for FILE in ${DIR_PATH}/${FILE_PATTERN}; do
 
	FILENAME=$(basename ${FILE})
	echo "Reading ${FILENAME}"
 
	OLD_IFS=$IFS; IFS=$'\n';
	while IFS="=" read -r PROP_KEY PROP_VALUE || [[ -n "$PROP_KEY" ]]; do
 
		echo "Prop Key:(${PROP_KEY}), (${PROP_VALUE})"
 
	done < ${FILE}
	IFS=${OLD_IFS}
 
done

where:

  • the first FOR iterate over a list of ini file in the current directory.
  • the while read:
    • reads a line of ${FILE} until it finds an EOF
    • parse it with the separator character specified with IFS (Field Separator) IFS=“=”
    • and set the consecutive variable PROP_KEY and PROP_VALUE
    • without using \ as escape character because of the r option -r
  • || [[ -n "$PROP_KEY" ]] permits to read the last line even without end of line
Advertising

3.2 - Csv

DIR_PATH="."
FILE_PATTERN="*.csv"
for FILE in ${DIR_PATH}/${FILE_PATTERN}; do
 
	FILENAME=$(basename ${FILE})
	echo "Reading ${FILENAME}"
 
	OLD_IFS=$IFS; IFS=$'\n';
        # Adapt to the number of col. Below you have 3
	while IFS="," read -r COL1 COL2 COL3 || [[ -n "$COL1" ]]; do
 
		echo "${COL1}, ${COL2}, ${COL3}"
 
	done < ${FILE}
	IFS=${OLD_IFS}
 
done

4 - Documentation / Reference