Swapping lines

Hi there,

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.

Hi, try:

awk '{print $3,$2,$1}' RS= FS='\n' OFS='\n' ORS='\n\n'  file

it works!.. however I don't know how to make it work for the whole file. After the first three lines it stops.

awk '{k=1;j=1;for (i=1;i<=NR;++i) k=i+2;j=i+1;print k,j,i}' RS= FS='\n' OFS='\n' ORS='\n\n' file

I tried this unsuccessfully again.

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

Try this modification:

awk '{i=1;j=i+1;k=i+2;print $k,$j,$i}' RS= FS='\n' OFS='\n' ORS='\n\n' file

@Scrutinizer Unfortunately doesn't work either. I've tried a few modifications but it only prints the first three lines.

@MadeInGermany It works, but for some reason the text is all the way around. The comments at the beginning show at the end.

I don't want to bother you guys that much. I thought this could be easier....

how about:

awk '{for(i=NF;i;i--) print $i; print RS}' RS= FS='\n' myFile

If not, provide the output of cat -vet myFile using code tags.
Also, what OS are you on?

1 Like

Thanks. My bad. Those previous solutions work.

I was testing it with the wrong file. My apologies.

Thanks again for the help. I've learned a lot.

Or try sed:

sed -n 'h;n;G;h;n;G;$!N;p' file