I have a text that I'm trying to format into something more readable. However, I'm stuck in the last step. I've searched and tried things over the internet with no avail.
OS: Mac
After parsing the original text that I won't put here, I managed to get something like this, but this isn't what I need yet.
1 [date]
2 [Comments]
3 [X says:]
The problem is that this text is kind of upside down.
Desired output:
1 [X says:]
2 [Comments]
3 [date]
What I've tried so far is embarrassing ..
awk '/says:/ {NR>2;c=$0;print c,NR-2}' file
This won't work. I've tried many others examples on the internet but they didn't work and I quite didn't understand them either.
An original sample file input would be:
2018-09-09T14:40:18.161Z
Hello
Pat says:
2018-09-09T14:40:29.813Z
How are you doing?
Pat says:
2018-09-09T14:40:46.808Z
I don't like it today.
Pat says:
2018-09-09T14:45:43.886Z
I wish I could go home.
Pat says:
2018-09-09T14:46:14.643Z
Could you help me?
Pat says:
2018-09-09T15:38:41.817Z
I can't.
Mark says:
The desired output should be something like:
Pat says:
Hello
2018-09-09T14:40:18.161Z
Pat says:
How are you doing?
2018-09-09T14:40:29.813Z
Pat says:
I don't like it today.
2018-09-09T14:40:46.808Z
Pat says:
I wish I could go home.
2018-09-09T14:45:43.886Z
Pat says:
Could you help me?
2018-09-09T14:46:14.643Z
Mark says:
I can't.
2018-09-09T15:38:41.817Z
This is the log of a conversation that I need to convert into a decent, readable form for others. Anyways, any help is much appreciated.
Another attempt with awk.
Untested but well documented.
awk '
# prepend the current line to buf (ORS is a newline)
{buf=($0 ORS buf)}
# new paragraph? Then store the current line (replacing old content)
p==0 {buf=$0; p=1}
# empty line? Then print buf; new paragraph
NF==0 {print buf; p=0}
# at the END print buf if not yet printed
END {if (p==1) print buf}
' file