Shell Script to Parse CSV Files In Linux
Author - Sanjay
Introduction
We will now look at creating a script to parse and format a CSV file. The formatting of the file will add new lines, tabs, and color to the output, so that it is more readable. We can then use grep to display single items from the CSV file. The practical application here is a catalog system based on the CSV files.
Table of Contents
Pre-Requisistes
A Linux server or a Unix System.
Also Read
- Grep Command Example Linux
- Secure Your Apache Web Server on UNIX / Linux
- Install and Configure Nginx On Rocky Linux 8
- Grep Command Examples You dont Know
Parsing CSV FIles
The CSV file or list of comma separated values will come from the file tools that we have in a current directory. This is a catalog of products that we sell. The file contents are shown in the following output:
menstshirt,99,5
tv,10,50
refrigerator,5,100
laptop,25,30
mobile,5,23
table,1099,3
Each line represents the catalogue with the values
- Name of Product
- Price
- Price Amount
We are having a menstshirt that costs $99 and having 5 Units in stock .
Writing Script to Parse CSV Files
Now , that we have the data ready we can parse the CSV files , lets write the script .
#!/bin/bash
OLDIFS="$IFS"
IFS=","
while read productName price stock
do
echo -e "\033[1;33m$productName \
========================\033[0m\n\
Price : \t $price \n\
Quantity : \t $stock \n"
done <"$1"
IFS=$OLDIFS
Let's explain the script we have written .
- We are going to accept the file name as argument hence we have used
$1
. - The IFS variable stores the file separator and this is normally a white space. We can store the old IFS so that we can restore it later at the end of the script. Ensuring that we return the same environment once the script is complete, no matter how the script is run.
- We have written a while loop to populate three variables that we need: productName, price, and stock. The while loop will read the input file, line by line, and populate each of the variables.
- The echo command with
033
and variable$productName
displays the product name in blue with double underscores underneath. The other variables are printed on new lines and tabbed in.
Let's run the script as shown below
Now that we have created the shell script , i will show you how can use grep command along with the output to search for desired searches. Have a look at grep command examples for more use cases like this .
Conclusion
We have seen how can we write simple shel script to parse csv files . We have used the sample csv to parse and update .