view the column names for CSV file?
Solution 1
head -n 1 file.csv
or
sed 1q file.csv
or
grep -m 1 '' file.csv
or
awk 'NR==1 {print; exit}' file.csv
Solution 2
You can use csvtool
to obtain column names. First install it from Ubuntu repository:
apt install csvtool
Then run it on the csv file:
csvtool head 1 file.csv
Output:
name1,name2,name3
If You need each column name in a separate line use tr
command like this:
csvtool head 1 file.csv | tr -s ',' '\n'
name1
name2
name3
Solution 3
While the answer for awk
or sed
or grep
above is amazing, I would like to offer an alternative solution for those people who are not familiar with queries. You can use a Relational Database Management System (RDBMS) or some other tools, such as Acho, to process your CSVs.
How to upload a CSV and view the column name
-
Upload your CSV using Add Resource
You can choose CSV or MCSV to upload, just make sure that all the CSVs have the same data schema (same column name, column number, column data type) if you are using MCSV.
-
CSV Preview
After the file selection, you can use the CSV Preview mode to see all your column names and columns. Additionally, you can choose to turn on the Auto-detect Schema or the Safe Mode. Auto-detect Schema can automatically detect the CSV's column name and column types for you. If the 1st upload fails, try to upload it with the *Safe Mode on, which turns all the column data types into "string" for the CSV.
If your CSV doesn't consist of any column name in the header row, just click Add a header row and edit those column names then. This way, your CSV will have the 1st row as its column name, which is super beneficial for your later data work.
-
Add the CSV into a project and have the whole table
After the CSV has been successfully uploaded, just add this CSV resource into a project for the data manipulation. Then open it in the project. You can easily see your CSV presented in a table with all its column names.
Related videos on Youtube
Qassam Mahmoud
Updated on September 18, 2022Comments
-
Qassam Mahmoud over 1 year
i have a CSV file i want to get the columns name for each column
sample :
how can i do that with awk or sed or grep ??
-
Cyrus almost 6 yearsPlease add your desired output for that sample input to your question.
-
-
dessert almost 6 yearsAs
print
is the default action you can shorten theawk
command toawk NR==1 file.csv
. -
Cyrus almost 6 years@dessert: Thank you. I'm aware of that. This way awk reads only the first line of the file.
-
dessert almost 6 yearsI don’t understand –
awk 'NR==1 {print; exit}'
does the exact same asawk NR==1
. -
αғsнιη almost 6 years@dessert when
awk NR==1
, still awk will read your whole file's lines where those are not necessary to read. Also that can be onlyawk '{print; exit}'
-
fiatux almost 6 yearsor
awk '1;{exit}'