Skip to main content
Bash ScriptingCentOS

Working with CSV files — Bash Scripting

By April 16, 2013September 12th, 2022No Comments

Now we know how to format the output in color, lets see what we may use this for. Searching though CSV files and highlighting names or products is one option and we will look at a script that searches a user directory in CSV format for users and their job roles. The GNU command grep will be useful to us in this search for -A3 , rows after and -B1 rows before.

We have been used to using read in scripts before but we can populate more than one variable if needed:

while read user job uid location

Here we read in our loop 4 variables: user through to location , these make up the schema of the CSV files that we read. The whole script is below :

		IFS=","
		while read user job uid location
		do
		  echo -e "e[1;33m$user 
		  =======================e[0mn
		  Role : t $jobn
		  ID : t $uidn
		  SITE : t $locationn"
		done < $1

We read the input parameter $1 into the looping structure, this will need to be file-name of the csv file to read.