0

Basically the title says it all. I could do with some help in writing a little bash or ksh script that can check the length of every field in a .csv file and quit the script with an error if a field length is over 50 characters.

I have already done a quick bit of research but haven't found anything that's exactly what i want!

Thanks in Advance!

alyr1481
  • 31
  • 5
  • 2
    Please show what you've tried, we're not here to do your work for you. This should be easy to do in `awk`. – Barmar Feb 22 '15 at 12:26
  • The problem with CSV is that it's *deceptively* simple, but in reality have many though corner cases. For example, if a field can contain quoted strings those strings can contain the field separator which needs to be handled, or handling escaped field separators, not to mention detecting records with wrong number of fields or erroneous data. – Some programmer dude Feb 22 '15 at 12:30
  • And multiline records are also relevant, because some fields may contain \n (newlines). So you should not really believe it is deceptively simple. Specially because it is not really a standard and many, many variants do exist to the primary concept of comma separated values. – lnrdo Feb 22 '15 at 12:43

1 Answers1

2

—How to deal with CSV files in bash? —You do not. PERIOD

Unless you are generating the CSV yourself you should not do it with bash alone as the CSV format can be tricky.

The naïve answer would be to split each line on commas, but this is not robust and you should probably use perl's Text::CSV (package libtext-csv-perl on Debian and derivatives).

You may install some CSV parsers to help you with this job:

You probably should probably move to a general purpose language like Perl or Python to accomplish this task.

Community
  • 1
  • 1
lnrdo
  • 396
  • 1
  • 13