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.
wert,der_tran,der_id,der_version,cvns_num,cvs_type
AB42126325,0,694698683,0,651626843,13002
AB42126326,0,694698686,0,651626846,13001
You could try something like:
#!/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:
./tester
or
./tester der_id
and it will create a file named der_id.txt
containing:
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.
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.