How to replace my file containing records that is delimited by , (asterisk, comma, asterisk) to pipe?
Thank you very much!
Here is the sample data:
0123456789″,”WLAN”,”0123456″,”012345678″,”1.00″,”AB”,”ABCD EFGHIJ”,”0123
Expected output is:
0123456789|WLAN|0123456|012345678|1.00|AB|ABCD EFGHIJ|0123
I have already tried sed, awk and perl but to no avail. This file contains around 50M records.
engrisaiah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Using sed with a character set:
$ sed -E 's/[,"]+/|/g' file
0123456789|WLAN|0123456|012345678|1.00|AB|ABCD EFGHIJ|0123
If the delimiter includes the quotes:
sed 's/","/|/g' <<<'0123456789","WLAN","0123456","012345678","1.00","AB","ABCD EFGHIJ","0123'
0123456789|WLAN|0123456|012345678|1.00|AB|ABCD EFGHIJ|0123
If you don’t have embedded double quotes or comma then this would do the trick:
$ sed 's/"//g; s/,/|/g' input.txt
0123456789|WLAN|0123456|012345678|1.00|AB|ABCD EFGHIJ|0123
Otherwise you are better off using something that can parse CSV. For example after I add the missing double quotes you can use csvtool:
$ csvtool -u '|' cat input_fixed.txt
0123456789|WLAN|0123456|012345678|1.00|AB|ABCD EFGHIJ|0123
csvkit’s csvformat command:
$ csvformat -D '|' input.txt
0123456789|WLAN|0123456|012345678|1.00|AB|ABCD EFGHIJ|0123