How to read a CSV or a INI file with Bash

Bash Liste Des Attaques Ovh

About

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

Snippet

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 Bash - 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

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

Documentation / Reference





Discover More
Bash Liste Des Attaques Ovh
Bash - While

This article is dedicated to the while syntax in Bash. where: : is the no-op command; its exit status is always 0 The command: lists the file with the ls executable (ls -l) pipe the...
Bash Liste Des Attaques Ovh
Linux - File

Linux file management See Using Parameters Expansion Removal From a path string where the file does not exist dirname returns the first parent of an existing path file. ...



Share this page:
Follow us:
Task Runner