Question

I have the comma separated flat file whose contents are below.

Name,Empno,Address,Contact,Company
A,1,”BTM, Bangalore”,1234,”HCL”
B,2,”Tatanagar”,1243,”WIPRO”
C,3,”Patna”,1254,”CTS”
D,4,”Doranda,Ranchi”,1253,”TCS”

I need to get only Name and Contact for everyone. If I use delimiter as “,” the contact for A and D will be Bangalore and Ranchi which is incorrect. How can I get the 1st and 4th field.Also Please advise if I can use awk command to get the required details

EDIT Just to add this is sample data, my original data will have some more fields, also there can be comma at any field which is within quotes.

Was it helpful?

Solution

I assume contact is always last but one field. You can try this

awk -F, '{if(NR>1)printf("%s,%s\n",$1,$(NF-1));}' ip.txt

OTHER TIPS

$ awk -F, '{print $1, $(NF-1)}' csv.txt 
Name Contact
A 1234
B 1243
C 1254
D 1253
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top