Listing column names in CSV file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Listing column names in CSV file
# 1  
Old 04-08-2016
Listing column names in CSV file

Hi,

I have a .csv file that has ~600 columns and thousands of rows. I would like to create a numerical list of the column names (so that I can later easily select the columns I want to extract). The format that I would hope for is something like:

Code:
1   "ID"
2   "X" 
3   "Y" 
..
600 "Z"

Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!


I have tried various sed / awk commands that I have found online, but nothing has really produced what I need.. I know this is very basic, but would be very grateful for your help. Thank you.

Last edited by RudiC; 04-08-2016 at 06:52 AM.. Reason: Added code tags
# 2  
Old 04-08-2016
Please show us (some of) your attempts!
# 3  
Old 04-08-2016
Here is the command that I thought was most promising:

Code:
awk 'BEGIN{ FS="|" }
       { for(fn=1;fn<=NF;fn++) {print fn" = "$fn;}; exit; }
      ' filename.csv

But all I got was:
Code:
1 = "column1name", "column2name", "column3name", etc.

Rather than:

Code:
1 = "column1name"
2  = "column2name"

... etc, which is what I am after.

Moderator's Comments:
Mod Comment No nonsense: Please use code tags as required by forum rules!



Any ideas?

Last edited by RudiC; 04-08-2016 at 07:09 AM.. Reason: Added code tags (again!)
# 4  
Old 04-08-2016
So the columns seem to be separated by commas, not pipes. How about setting the field separator FS=","?
# 5  
Old 04-08-2016
Silly me. That now works.
Thank you very much for your help.
AB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare every column from one csv file to another csv file

1.csv contains following column- Empid code loc port 101 A xy 01 102 B zx 78 103 A cg 12 104 G xy 78 2.csv contains follwing data- Empid code loc port 101 A gf 01 102 B zx 78 103 C cg 32 104 ... (1 Reply)
Discussion started by: rishabh
1 Replies

2. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Matching the header of a .CSV file with dynamic field names

I have a .CSV file (frequency - weekly) whose header contains the year-week value in two of the columns which keeps changing every week. For an instance please see below. Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10,Column11,Column12,Column13,201420... (4 Replies)
Discussion started by: dhruuv369
4 Replies

5. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

6. Shell Programming and Scripting

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

7. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

8. Shell Programming and Scripting

Listing file names as soon as they are created

Hi, I have been looking for a method to list file names as soon as they are created. I have used the following command : find . -name "*.xml" -mmin -2 -exec ls --full-time {} \; | sort -k6 this finds all xml files created in the last 2 minutes and orders them by time. The problem is that... (7 Replies)
Discussion started by: phil.e.b
7 Replies

9. UNIX for Dummies Questions & Answers

Listing full file names with the exact length of 3 characters

This is what I have to do: Display the full file name (including the full path) and file size of all files whose name (excluding the path) is exactly 3 characters long. This is the code I have: find / -printf "Name: %f Path: %h Size: %s (bytes)\n" 2>/dev/null | grep -E "Name: .{3,} Path" |... (7 Replies)
Discussion started by: Joesgrrrl
7 Replies
Login or Register to Ask a Question