0

I have a program that outputs many lines of data in a space-separated table. I need to convert this output to a semicolon-separated list.

This is an example record. Each field has a set number of characters, padded by a set number of spaces, however, the amount of characters in a field and padding spaces depends on the field, as shown below:

FIELD.FIELD2x...FIELD3xxxx.....FIELD4xxxxxxxxx.....FIELD5xxxxxxxxx....<etc>
FIELD.FIELD2x...FIELD3xxxx.....FIELD4xxxxxxxxx.....FIELD5xxxxxxxxx....<etc>
FIELD.FIELD2x...FIELD3xxxx.....FIELD4xxxxxxxxx.....FIELD.WithSpace....<etc>
FIELD.FIELD2x...FIELD3xxxx.....FIELD4xxxxxxxxx.....FIELD5xxxxxxxxx....<etc>

Normally, it wouldn't be an issue to just do something like

command | tr -s ' ' ';'

Which would ideally yield something like this:

FIELD;FIELD2x;FIELD3xxxx;FIELD4xxxxxxxxx;FIELD5xxxxxxxxx;<etc>
FIELD;FIELD2x;FIELD3xxxx;FIELD4xxxxxxxxx;FIELD5xxxxxxxxx;<etc>

However, as detailed above, the fields are allowed to contain spaces themselves, and to complicate things they are printed without quotes. Therefore, running the above "tr -s ' ' ';'" command actually does this:

FIELD;FIELD2x;FIELD3xxxx;FIELD4xxxxxxxxx;FIELD5xxxxxxxxx;<etc>
FIELD;FIELD2x;FIELD3xxxx;FIELD4xxxxxxxxx;FIELD;WithSpace;<etc>
                                              ^ <-- Problem here :(

I cannot for the life of me figure out how to create a semicolon-separated list from this data without wrecking the fields that have spaces in their values. If anyone has an idea of how to solve this problem, that would be absolutely awesome!

3
  • 2
    Welcome, please post a workable sample. – schrodigerscatcuriosity Jan 19 at 3:31
  • Hi @schrodigerscatcuriosity, I'm not quite sure what you mean. If you'd be willing to elaborate, I'd be happy to do so :) – ctx400 Jan 19 at 4:19
  • 1
    @ctx400 workable sampe = a few lines of the actual file (including problematic space-in-field-lines) so people may test their code ideas. – FelixJN Jan 19 at 10:35
0

I think you should pre-process all the field values first, try to detect format of the field & translate it to regex then normalize the text before adding delimiters.

FIELD FIELD2x   FIELD3xxxx         FIELD4xxxxxxxxx       FIELD5xxxxxxxx    <etc>
FIELD FIELD2x   FIELD3xxxx         FIELD4xxxxxxxxx       FIELD5xxxxxxxx    <etc>
FIELD FIELD2x   FIELD3xxxx         FIELD4xxxxxxxxx       FIELD 7xxx    <etc>

Regex Field Format:

(FIELD[\s\dx]+)

==> Using sed / awk ... to update to new field format ==> Adding delimiters.

2
  • hmm, interesting... Let me try this real quick! – ctx400 Jan 19 at 4:31
  • Ok, I see what you're getting at! I tried experimenting on regexr.com, and I settled on the pattern ([\w\d\/\:\.]+\s)+. I'll probably have to convert it to awk or sed format, but you answered my question! Thanks! Now that the fields are separated, each match can just be appended to the semicolon-separated list. – ctx400 Jan 19 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.