-1

I have file in linux with comma delimited and string fields in double quotations ", I need to convert them to pipe delimiter please share your inputs.

Example:

Input:

"2017-09-30","ACBD,TVF","01234",NULL,18,NULL,"686091802","BANK OF ABCD, LIMITED, THE",790456

Output:

2017-09-30|ACBD,TVF|01234|NULL|18|NULL|686091802|BANK OF ABCD, LIMITED, THE|790456

Thanks In advance...

Thanks,

anubhava
  • 713,503
  • 59
  • 514
  • 593
user2601350
  • 207
  • 1
  • 3
  • 12

4 Answers4

2

Here is a solution using gnu awk using FPAT variable:

awk -v OFS='|' -v FPAT='"[^"]*"|[^,]*' '{
   for (h=1; h<=NF; h++) printf "%s%s", $h, (h < NF ? OFS : ORS)
}' file

"2017-09-30"|"ACBD,TVF"|"01234"|NULL|18|NULL|"686091802"|"BANK OF ABCD, LIMITED, THE"|790456

Working Code Demo

anubhava
  • 713,503
  • 59
  • 514
  • 593
1

You would be well-served to use a proper CSV parser. Here's a Ruby example:

ruby -rcsv -e '
  f = ARGV.shift
  csv_out = CSV.new $stdout, :col_sep => "|"
  CSV.foreach(f) {|row| csv_out << row}
' file.csv

outputs

2017-09-30|ACBD,TVF|01234|NULL|18|NULL|686091802|BANK OF ABCD, LIMITED, THE|790456
glenn jackman
  • 223,850
  • 36
  • 205
  • 328
0

Easiest probably to use sed

PIPE_SEP_STR=$(echo $COMMA |sed -e 's/,/|/g')

where COMMA is the variable which contains your comma seperated string

0
awk '{sub(/"/,"")gsub(/",|,"|","/,"|")sub(/,18,/,"|18|")}1' file

2017-09-30|ACBD,TVF|01234|NULL|18|NULL|686091802|BANK OF ABCD, LIMITED, THE|790456
Claes Wikner
  • 1,397
  • 1
  • 8
  • 7