0

This probably sounds contradicting. So let me explain. I have a number of log files that use log4j to write to different files and rotate. What I want to do is merge them into fewer files.

How I started to go about doing this:
- use awk to concat multi-line entries into one line into a separate file.
- cat awk output files to 1 file.
- sort the cat file
- awk to separate the concatenated lines.

But I see that the sort is putting entries with the same second/ms in a different order than they appeared in their original output file. It may not be a HUGE deal. But, I don't like it. Any ideas for how I go about doing what I want (maintaining their original line order while sorting)? I would rather not write my own program and would like to use native linux utils if possible. But, I am open to the "best" way of doing this (Perl, Python, etc..).

I thought about cat'ing the output files from highest to lowest (log4j rotate files) so that I wouldn't have to sort. But that only solves the problem for files writing to the same log file (file1.0.log, file1.1.log, etc..). But this doesn't help when needing to merge file2 with file1.

Thank you, Gregg

Gregg
  • 492
  • 2
  • 7
  • 16

1 Answers1

1

What you are talking about is "stable" sorting. There is a -s option on sort that should give you what you want.

Stability in sorting algorithms

Community
  • 1
  • 1
Jeff Y
  • 2,437
  • 1
  • 11
  • 18
  • Thanks. That did it. It originally returned unexpected results. But that was based on the "key" being used and having to adjust accordingly with sort -k. Cheers! – Gregg Mar 01 '16 at 16:21
  • Good deal. (Yes, one thing that is easy to get caught by is that `-k` keys always have a beginning and an end, and the default when `,end` is not specified is end-of-line.) – Jeff Y Mar 01 '16 at 16:24