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!