Saminator
Saminator

Reputation: 77

How to find-replace delimiter inside a column in unix file

I have a unix file and ',' is the delimiter with 7 columns. How could i replace a column lets say column 3 which has ',' in it to some other character?

ex: col1,col2,col3,col4... 100,'xyz','sample,data',5000,

replace , in sample,data to some character.

Thanks

Upvotes: 0

Views: 1853

Answers (1)

You can use sed to replace the Nth occurence of a pattern with the syntax sed 's/from/to/N', as in:

sed 's/,/@/3' <<<"100,'xyz','sample,data',5000"

Output:

100,'xyz','sample@data',5000

Note that this will replace exactly the third comma it finds to @. This implies that if there isn't a comma in column 3 then it will replace the separator between columns 3 and 4. And if there is a comma in an earlier column, then it will change that instead of the one in column 3.

I'm not sure how unix could distinguish between field separator commas and commas inside your fields. Maybe we could keep count of the number of single quotes, but we'd need to know more about your files to give a full solution.

Upvotes: 2

Related Questions