I have a comma-separated file with 7 fields. One of these fields, however, contains , in the value. I want to change the delimiter of the file to | without changing the data of the field with the ,.
File I have:
Name,Age,Country,ID,Address,Category,DOB
John Doe,19,England,3653,Manchester, England,Main Worker,20-05-1995
Required Output:
Name,Age,Country,ID,Address,Category,DOB
John Doe|19|England|3653|Manchester, England|Part Time Worker|20-05-1995
I have tried multiple solutions but have not been able to achieve what I am looking for. I tried the following command which only updates the delimiter of the first 4 columns:
sed 's/,/|/;s/,/|/;s/,/|/;s/,/|/' file
The approach I'm looking for is to update the delimiter of the first 4 columns and last 2 columns. In this way I can have the file with updated delimiter without changing the Address column.
I have come up with the following piece of code but this removes the , between Manchester and England.
awk '{ORS="";N=split($0,a,",");\
print a[1]"|"a[2]"|"a[3]"|"a[4]"|"; \
for(i=5;i<N-1;i++) print a[i];
print "|"a[N-1]"|"a[N] }'
,to keep and which,to replace with|? Can we safely assume that if the current line has >10 fields, then we need to consider the 5th,as non-delimiter?,that comes before a space should not be considered a delimiter?sed -e '1!s/,/|/5' -e 'y/,|/|,/' bad.csv