Search code examples
bashawktext-parsingseparator

Transform a pretty-printed table to a single line with separators, using Awk


Trying to clean up output from a Python client. This is an example:

+--------------------------+-----------+
| Text                     | Test      |
+--------------------------+-----------+
| 111-222-333-444-55555555 | 123456789 |
| 111-222-333-444-55555555 | 123456789 |
| 111-222-333-444-55555555 | 123456789 |
+--------------------------+-----------+

I started by removing the top and bottom by piping the output with:

Command_Output | tail -n +4 | head -n -1 |

So now we have the following:

| 111-222-333-444-55555555 | 123456789 |
| 111-222-333-444-55555555 | 123456789 |
| 111-222-333-444-55555555 | 123456789 |

Now I'm trying to remove the pipes in the table and convert the table to a single comma-separated line. It's important to still keep the correlation between the two numbers, though, so maybe I should use two delimiters. Perhaps the final output should look like the following:

111-222-333-444-55555555~123456789,111-222-333-444-55555555~123456789,111-222-333-444-55555555~123456789

So now I'm at this point:

Command_Output | tail -n +4 | head -n -1 | awk '{$3 = "~"; print $0;}'

Can someone help me with the last part? I need to get the table into a single, comma-delimited line.


Solution

  • Command_Output | tail -n +4 | head -n -1 | awk -vORS=, '{ print $2 "~" $4 }' | sed 's/,$/\n/'
    

    Thanks for the help