BASH Programming

Read the CSV File in Bash

The full form of CSV is Comma Separated Value. The CSV file is used by the coder for many purposes that store the data in semi-structures tabular format. Each line of the file is treated as a row of the table and each field of the row is separated by a comma (,) in the CSV file. Many ways exist in Bash to read the CSV files which are explained in this tutorial.

Prerequisites:

You have to create a CSV file before practicing the example of this tutorial. Create a CSV file named “customers.csv” with the following content to check the output of the script that is used in this tutorial. In this file, the 3rd fields of the 4th line and 6th line are empty.

ID, Name, Email, Address, Mobile

101, Jafar Iqbal, jafar@gmail.com, 9/A Dhanmondi Dhaka, +8801762341425

102, Kamal Hossain, kamal@gmail.com, 120 Mirpur Dhaka, +8801988675345

103, Nirob Chowdhury,,33/2 Jigatola Dhaka, +8801754532312

104, Farheen Hasan , farheen@gmail.com<a href="blank">,</a> 10 Kadhalbagun Dhaka, +8801512875634

105, Md. Rahim,, 2/B Dhanmondi Dhaka, +8801700453423

Different Ways to Read the CSV File in Bash

The CSV file can be parsed in different ways using a Bash script. Different ways to read the “customers.csv” file are shown in this part of the tutorial.

Example 1: Read the Original Content of the CSV File

Create a Bash file with the following script that reads the full content of the “customers.csv” file using the “while” loop:

#!/bin/bash

#Set the filename

filename="customers.csv"

#Read each line of the file in each iteration

while read data

do

#Print the line

echo $data

done < $filename

The following output appears after executing the script:

Example 2: Read the CSV File by Capitalizing the Header

The first line of the “customers.csv” file contains the heading of the file. Create a Bash file with the following script that prints the content of the “customers.csv” file after capitalizing the first line of the file. The “awk” command is used in the script to print the content of the file after capitalizing the header. The comma(,) is assigned in the FS and OFS values in the script to read the “customers.csv” file and write the “updatedcustomers.csv” file. The “cat” command is used to print the content of both files.

printf "Original File:\n"

#Print the original content of the CSV file

cat cstomers.csv

#Create a new CSV file after capitalizing the header

awk 'BEGIN{FS=",";OFS=","}

{

if(NR==1)

print toupper($0)

else

print

}'
customers.csv > updatedcustomers.csv

printf "\nModified File:\n"

#Print the new CSV file

cat updatedcustomers.csv

The following output appears after executing the script:

Example 3: Replace the Empty Field of the CSV File with “None”

Create a Bash file with the following script that prints the content of the “customers.csv” file after modifying the empty field with the “None” value. Two fields are empty in this file which are mentioned in the following. The “awk” command is used in the script to print the content of the file after modifying the empty fields. The comma(,) is assigned in the FS and OFS values in the script to read the “customers.csv” file and write the “updatedcustomers.csv” file. The “cat” command is used to print the content of both files in the tabular format.

printf "Original File:\n"

#Print the original content of the CSV file in tabular form

cat customers.csv | column -s, -t

awk 'BEGIN{FS=",";OFS=","}

{

for(field=1;field<=NF;field++)

{

if($field == "") $field="None"

}

print

}'
customers.csv > modifiedcustomers2.csv

printf "\nModified File:\n"

#Print the new CSV file in tabular form

cat modifiedcustomers2.csv | column -s, -t

 

The following output appears after executing the script:

Example 4: Print the Total Number of Rows and Columns of the CSV File

Create a Bash file with the following script that counts the total number of rows and columns in the “customers.csv” file. The NR variable is used to print the total number of rows of the file. The NF variable is used to print the total number of fields of the file.

printf "Original File:\n"

#Print the original content of the CSV file

cat customers.csv

echo

echo -n "Total rows:"

awk -F, 'END{print NR}' customers.csv

echo -n "Total columns:"

awk -F, 'END{print NF}' customers.csv

The following output appears after executing the script. The total lines in the file is 6 and the total fields of the file is 5 which are printed in the output:

Conclusion

The methods of reading a CSV file, modifying the CSV file, and counting the rows and columns of the CSV file using the Bash script are shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.