1

I have a temporary file generated by a cron job every half hour that generates output like this:

---- kind=<data> field=<value> humankind=<data> kind=<data> field=<value> humankind=<data> kind=<data> field=<value> humankind=<data>

I want to format it so it looks like this:

---- kind=<data> field=<value> humankind=<data>
kind=<data> field=<value> humankind=<data>
kind=<data> field=<value> humankind=<data>

I have tried the following sed expressions:

sed -r 's|\s(kind=)|\n\1|g' /path/to/file

But then the output looks like:

----                                         <------ The first line should be here
kind=<data> field=<value> humankind=<data>
kind=<data> field=<value> humankind=<data>
kind=<data> field=<value> humankind=<data>

Using this expression:

sed -r 's|[^-]{4} (kind=)|\n\1|g' /path/to/file

Generates the output I want, but with weird behavior:

---- kind=<data> field=<value> humankind=<incomplete data>
kind=<data> field=<value> humankind=<incomplete data>
kind=<data> field=<value> humankind=<incomplete data>

For some reason, the last field (which contains part of the pattern used in the sed expression) is only printing the first two characters of 'incomplete data'.

What am I doing wrong?

1 Answer 1

1

The problem with sed -r 's|\s(kind=)|\n\1|g' /path/to/file is that you insert a new line for all ' kind' occurrences, including the first, so you get an extra newline after ----. Instead, try

sed -E 's|\s(kind=)|\n\1|2g' /path/to/file

as it will skip the first match.

The second one fails because it matches 4 chars before ' kind', and that is part of your data (which must be 6 chars long)!

1
  • That did it! Thank you!
    – AnthonyBB
    Mar 9, 2020 at 15:55

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.