Solved

Need help sorting a text file

Posted on 2006-12-01
5
203 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
qos on linux 3 108
what is Amazon Linux AMI 6 74
Subsystem for UNIX-based Applications (SUA) 2 92
busybox bash/ash - bash -x | grep 19 177
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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