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.
A Linux server or a Unix System.
- 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
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:
Each line represents the catalogue with the values
- Name of Product
- Price Amount
We are having a menstshirt that costs $99 and having 5 Units in stock .
Now , that we have the data ready we can parse the CSV files , lets write the script .
while read productName price stock
echo -e "\033[1;33m$productName \
Price : \t $price \n\
Quantity : \t $stock \n"
Let's explain the script we have written .
- We are going to accept the file name as argument hence we have used
- 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
$productNamedisplays the product name in blue with double underscores underneath. The other variables are printed on new lines and tabbed in.
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 .
We have seen how can we write simple shel script to parse csv files . We have used the sample csv to parse and update .