2

I have a pipe delimited file with the following syntax:

|ID Number|First Name|Middle Name|Last Name|#, Street, City|etc..

Some records are messy and I would like to have the strings be converted into title case. Based on other questions regarding converting the strings to title case, I've found this command:

awk  'BEGIN { FS=OFS="|" } {for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }'

Running that produces |Id number|First name|Middle name|Last name|#, street, city|, which capitalizes the first letter but since I have FS="|", The string in each field is being treated as one word.

I would like to have every thing inside each field to be title-cased, not just the first letter of each field.

If possible, I would like to have an awk only solution for this.

1 Answers1

3

Try something like this:

awk  'BEGIN { FS=OFS="\0" }
      {n = split($0, words, /[ |]/, separators);
       out = separators[0];
       for (i=1; i<=n; ++i) {
           out = out toupper(substr(words[i],1,1)) tolower(substr(words[i],2)) separators[i];
       };
       print out separators[n+1]; }' file
Michael Vehrs
  • 3,293
  • 11
  • 10