Solved

Need help sorting a text file

Posted on 2006-12-01
5
199 Views
Last Modified: 2010-04-22
I'm trying sort a log file (text file) from oldest to newest. Each line of the file has an output like:

service;username;release_number_number;environment;<date>;service-environment-<yyyyddmm>-<mm:ss>

the <date> format are like the following examples:
Fri Nov 17 06:33:13 PST 2006
Thu Nov 16 15:36:00 PST 2006

Here is an example of a real entry in my log file:
testapp;jhill;11_11-11;st;Tue May 30 12:35:00 PDT 2006;testapp-st-20060530-12:35

Ok so you get the idea.

Here's my issue. I have a log file similar to this on 2 different servers and I want to combine them.

I know i can cat one file and append to the other, but I want to them have in order as far as date. If I append 1 file to the other, what command could i use to sort?

Again, I'm spefically trying to sort the <date> field above. Thanks

One note, I did the following: sort -t\; -k5,5 inputfile , but that it by the time "12:35:00" time in the above example, not the entire date and time as specified in <date>. I'm not even sure if the sort command can do this.
0
Comment
Question by:pieman8080
  • 2
  • 2
5 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 18055622
cat file1 file2 | perl -ne 'split/\;/; $t=`date +%s -d "$_[4]"`; chop($t); print "$t:$_";' | sort -n | cut -d: -f2-
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 18055668
sort -n -t ";" -k 6,6 file1 file2 > file3

file3 is the combine log file.
0
 

Author Comment

by:pieman8080
ID: 18056179
Thanks ravenpl, that worked. I need one more thing. How do i eliminate mulpitle entries of the same line. There were some lines that were in both files so when i did the cat, it created multiple entries. thx
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 500 total points
ID: 18056192
cat file1 file2 | perl -ne 'split/\;/; $t=`date +%s -d "$_[4]"`; chop($t); print "$t:$_";' | sort -n | cut -d: -f2- | uniq
0
 

Author Comment

by:pieman8080
ID: 18056238
perfect, thank u very much
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now