Solved

Need help sorting a text file

Posted on 2006-12-01
5
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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