CSV files are commonly used to for data transfer and storage. They can be easily imported into most applications, and they are a popular format for data export too. But often you may find that CSV files don’t contain header row, that is, the row with column names or fields. This can be seriously annoying as we are unable to understand which value is meant for which column, while importing the file into other applications such as Excel or Access. In this article, we will learn how to add header in CSV file. You can do this in several ways. We will learn how to insert header row in CSV files using Linux commands. You can use these commands in almost every Linux platform. Of course, you can also use scripting languages such as python or perl for the same purpose. You can also do this using other data processing applications such as Excel and Access.
How to Add Header in CSV File Using Shell Script
Even in Linux, there are plenty of commands to help you add header row in CSV file. We will look at a couple of them. Let us say you have the CSV file data.csv as shown below.
Jim, NYC, 456434 John, San Francisco, 344544 Jane, Miami, 335435
Let us say you want to add the following header row as 1st row of the CSV file.
Name, Place, Zip code
1. Using sed
Here is the sed command to insert header row to your CSV file.
$ sed -i -e '1i<list of header columns>' /path/to/csv
sed is a powerful string editor used to easily modify texts in files. In the above command, we use -i option to edit the file in place instead of outputting the result on screen.
We use -e to use the expression within quotes for string editing. We specify 1i to insert at line 1.
Here is an example to add the required header using sed command.
sed -i -e '1i"Name","Place","Zip code"' /home/ubuntu/data.csv
2. Using Echo Command
You can also use echo command to insert header row into the file. Here is a sample command to do the same.
echo <list of header columns> > header.csv && cat /path/to/csv >> header.csv && mv header.csv /path/to/csv
The above command creates a file header.csv with a single row consisting of headers. It appends the content of CSV file data.csv to this file and then renames the header.csv file to your original CSV file, thereby overwriting it.
Here is an example to insert header row in data.csv file.
echo "Name","Place","Zip code" > header.csv && cat /home/ubuntu/data.csv >> header.csv && mv header.csv /home/ubuntu/data.csv
You can include the above commands in any shell script in case you want to automate it. For example, you can create an empty shell script file with the command below.
$ vi add_csv_header.sh
Add the following lines to it.
#!/bin/sh sed -i -e '1i"Name","Place","Zip code"' /home/ubuntu/data.csv
Save and close the file. If you want to use echo command instead of sed command, replace it in the above code.
Make the shell script executable with the following command.
$ chmod +x add_csv_header.sh
Now you can run the shell script with the following command.
$ ./add_csv_header.sh
Also read:
How to Create Yum Repository Using ISO Image
How to Setup Local Yum Repository in CentOS/RHEL
How to List Files Installed in RPM or DEB package
How to Change Console Fonts in Ubuntu
How to Set Password for Single User Mode in Linux
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.