0

I've got a set of files that contain notes written in reverse order:

###### 27/07/2016-15:04:20 GMT+1:
Really? More? 

###### 27/07/2016-15:04:18 GMT+1:
It happened again! 

###### 27/07/2016-15:04:16 GMT+1:
A thing happened

I'd like them to appear in this order:

###### 27/07/2016-15:04:16 GMT+1:
A thing happened

###### 27/07/2016-15:04:18 GMT+1:
It happened again! 

###### 27/07/2016-15:04:20 GMT+1:
Really? More? 

Some of these files have couple of hundred entries - what's the easiest way of making this happen?

1
  • The linux 'tac' command ('cat' backwords) will print out all of the lines reversed but then your groups would be reversed also. (Message after date)
    – P. Heffner
    Jul 27, 2016 at 14:33

2 Answers 2

3

Only tac

The command tac can do it.

$ tac -b -s "######" file.log

Call tac by Vim

If you want run it in Vim:

:%!tac -b -s "\#\#\#\#\#\#"

% means all lines.

! runs external command.

The backslash is necessary to escape the number sign # in "\#\#\#\#\#\#".

Only Vim

These commands replace new line by tab. If tab is present in the log, use another character instead of tab.

:%s/\n/\t/g
:%s/\t######/\r######/g
:g/^/m0
:%s/\t/\r/g
1

If there is a final newline at the end, the following command does what you want in Vim:

g/^###/.,/^$/m 0

Which searches for lines starting with ### takes the range from ### til the next blank line and moves it to the top.

If there is no final newline, it will probably abort with e.g. E16: invalid range, in that case you could adjust the regex like this:

g/^###/.,/\(^$\|\%$\)/m 0

However, du to the last line missing, the first entry won't have a new line afterwards, so after all it might be easier to just add a single empty line add the end and use the first command.

3
  • It is possible the range to go to another ######, something like :g/^###/,/^###/-1m 0? What is the purpose of the dot . in your code?
    – Marco
    Jul 27, 2016 at 19:28
  • :g/^###/;/\(\n###\|\%$\)/m0 worked for me.
    – Marco
    Jul 27, 2016 at 21:29
  • the dot means explicit this line. Yes you can change the pattern to match other lines if you want Jul 28, 2016 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .