Reading the value of particular column from csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading the value of particular column from csv file
# 1  
Old 11-30-2013
Wrench Reading the value of particular column from csv file

Hi Folks,

I have the below csv file which is comma delimited , now from this file i need to read the value of the column der_id and then want to create a separate text file which will contain the value of the column der_id only please advise how to read the value of the column der_id and then how to store it a separate file.Smilie

Code:
  wert,der_tran,der_id,der_version,cvns_num,cvs_type
    AB42126325,0,694698683,0,651626843,13002
    AB42126326,0,694698686,0,651626846,13001

# 2  
Old 11-30-2013
You could try something like:
Code:
#!/bin/ksh
awk -F, -v title="${1:-der_id}" '
NR == 1 {
        for(i = 1; i <= NF; i++)
                if($i == title) {
                        field = i
                        of = title".txt"
                }
        if(of == "") {
                printf("Field heading \"%s\" not found.\n", title)
                exit 1
        }
}
{       print $field > of
}' file.csv

If you save this in a file named tester and make it executable, and the name of your csv file is file.csv, you could invoke it as:
Code:
./tester
    or
./tester der_id

and it will create a file named der_id.txt containing:
Code:
der_id
694698683
694698686

If you invoke it with another column title it will create a file with a name that is that title followed by ".txt" and store the data from that column in that file. If you give it a column title that isn't found on the first line of the file, no output file will be created but it will print a diagnostic saying it couldn't find the field heading you specified.
# 3  
Old 11-30-2013
Try this

cat file.csv | awk -F"," {'print $3'} > required_out.csv
# 4  
Old 11-30-2013
Quote:
Originally Posted by KVignesh
Try this

cat file.csv | awk -F"," {'print $3'} > required_out.csv
No need to use cat and pipe., and I think user wants to read column by header name.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading CSV file

Hi experts, Im having csv file with few columns which should contain data as shown below. Want to check if column 3 contain row with duplicate value(9876,9876) then corresponding to this in col2 should contain text "tax" and should not contain text "non". Word "non" can come but if in column3... (2 Replies)
Discussion started by: as7951
2 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

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

(14 Replies)
Discussion started by: dhruuv369
14 Replies

5. 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

6. Shell Programming and Scripting

Reading from a CSV and writing in same CSV file

Hi, I am tryng to read from a csv file and based on some grep command output I will modify one of the column in the same csv. Example:- Input CSV:- 20120829001415,noneAA,google.com 20120829001415,dfsafds,google.com 20120829001415,noneAA,google.com Intermediate Step:- If 2nd column of... (3 Replies)
Discussion started by: kmajumder
3 Replies

7. Shell Programming and Scripting

Reading variables from CSV file

Hi I am using KSH and trying to read variables from a csv file. I've set the IFS=, and it workds. Problem is where one of the values is text containing a comma. For example the following lines exist in my file. How can I read everything between the quotes into a single variable? APW13812,,1... (2 Replies)
Discussion started by: ventris
2 Replies

8. UNIX for Advanced & Expert Users

Issue reading csv file

HI All I have csv file containing the data like this Electrical Equipment,ElecEquip "Engineering, Machinery & Equipment",Engineerin Entertainment & Broadcasting,Entertain The first and third record are fine,The issue with second records as it has comma enclosed with in inverted... (1 Reply)
Discussion started by: mohdtausifsh
1 Replies

9. Shell Programming and Scripting

reading from a .csv file

Hi , Can anyone please help me to read the value from the .csv file? This is my .csv file: dirnames: first,second i want to get the two names and create directories correspondingly (1 Reply)
Discussion started by: novice_user
1 Replies
Login or Register to Ask a Question