0

I am looking for any known tools or scripts which can convert my bcp files to csv files.

Input bcp file format:

  1. Fields separated by 'XXXXXXX'
  2. Rows separated by 'YYYYYYY'
  3. Fields contains special characters like CRLF,CR,LF,", Tab, comma etc...

Output format I want:

  1. Standard csv format file with comma delimited
  2. Field values should contain original content including special characters (I mean no addition or deletion of special character(s), CR should also not be deleted)
  3. The file able to cut by column index/name to select interested columns.

For this I did the following:

  1. Transformed the bcp file to csv with few sed commands, with this I can open the file in MS excel program with proper alignment, and I could see content was not altered (as expected).

    sed -i 's/\"/\"\"/g' $inFile

    sed -i 's/XXXXXXX/","/g' $inFile

    sed -i 's/YYYYYYY/"\n"/g' $inFile

    sed -i '1s/^/\"/' $inFile

    sed -i '$s/\"//' $inFile

    sed -i -e '${/^$/d}' $inFile

    sed -i '1s/^/"Header","added","here"\n/' $inFile

  2. Tried csvkit tool: csvcut $infile This tool is selecting the preferred columns but modifying the content like deleting the CR.

Any ideas in this kind conversion?

LaintalAy
  • 1,162
  • 2
  • 15
  • 26
Bhupal.Bobbadi
  • 61
  • 1
  • 1
  • 9

1 Answers1

-1

awk to the rescue!

awk -F='XXXXXXX' -v RS='YYYYYYY' -v OFS='","' -v ORS='\n' 
                  '{gsub(/"/,"\""); $1=$1; print "\""$0"\""}' file

escape quotes, quote fields, change field delimiter to comma and record delimiter to newline. $1=$1 forces awk to apply the new delimiters.

karakfa
  • 66,216
  • 7
  • 41
  • 56