csvkitHow to add a header to a CSV file using csvkit?
Adding a header to a CSV file using csvkit is a simple process.
Example code
csvcut -n myfile.csv
csvcut -c 1,2,3 myfile.csv > newfile.csv
echo "header1,header2,header3" | cat - newfile.csv > temp.csv
mv temp.csv newfile.csv
Output example
header1,header2,header3
data1,data2,data3
Code explanation
-
csvcut -n myfile.csv
: This command prints the column names of the CSV file. -
csvcut -c 1,2,3 myfile.csv > newfile.csv
: This command cuts the columns 1, 2 and 3 from the CSV file and stores them in a new file. -
echo "header1,header2,header3" | cat - newfile.csv > temp.csv
: This command adds the header to the new file and stores it in a temporary file. -
mv temp.csv newfile.csv
: This command moves the temporary file to the new file.
Helpful links
More of Csvkit
- How to convert a tsv file to csv with csvkit?
- How to select specific columns with csvkit?
- How to fetch unique values with csvkit?
- How to skip the first line with csvkit?
- How to change the encoding in csvkit?
- How to remove duplicates with csvkit?
- How to use csvkit on Windows?
- How to rename a column with csvkit?
- How to convert a json file to csv with csvkit?
- How to convert a csv file to tab delimited with csvkit?
See more codes...