TAKING LOG FILE AND ELIMINATING ALL DUPLICATES THEN SORT WHATEVER IS REMAINING IN TIME ORDER
Here is a script you can use afterwards, I will discuss what each command is doing
SCRIPT
#!/bin/bash
rm -f "$2" 2> /dev/null
touch "$2"
cat "$1" > tmp
sort -r tmp > "$1"
rm -f tmp 2> /dev/null
while read -r line; do
line_to_find=`echo "$line"|cut -d ' ' -f2- `
no_of_duplicated_lines=`grep "$line_to_find" "$1"|wc -l`
if [[ "$no_of_duplicated_lines" != @(1) ]]; then
matching_line_in_log_files=`grep "$line_to_find" "$2"`
if [ -z "$matching_line_in_log_files" ]; then
echo "$line" >> "$2"
fi
else
echo "$line" >> "$2"
fi
done < "$1"
cat "$2" > tmp
sort -r tmp > "$2"
rm -f tmp 2> /dev/null
HOW THE SCRIPT WORKS
my_script < log_file > < new_log_file >
"path to script" "path to log file to be modified" "a new log file location"
[STEP 1] REMOVING ANY EXISTING FILES THAT I HAS THE NEW FILENAME I WANT TO CREATE
rm -f "$2" 2> /dev/null
[STEP 2] CREATE A NEW LOG FILE FOR NO DUPLICATES AND SORTING OLD LOG FILE MESSAGES ACCORDING TO RECENT TIME
Open old log file and redirect it to a temporary file then sort all messages according to time. Afterwards, redirect all sorted messages back to old log file
cat "$1" > tmp
sort -r tmp > "$1"
rm -f tmp 2> /dev/null
[STEP 3] READ EACH LINE IN YOUR OLD LOG FILE
This is done by using a while do loop to read line until end of file
while read -r line; do
............
............
done < "$1"
[STEP 4] MODIFY EACH LINE READ BY ELIMINATING TIMESTAMP AND SAVE TO A VARIABLE
We already sort messages according to recent timestamp
line_to_find=`echo "$line"|cut -d ' ' -f2- `
[STEP 5] SEARCH LOG FILE FOR NO. OF DUPLICATED LINES AND NO DUPLICATE LINES in OLD LOG THAT HAS THE VARIABLE WITHOUT TIMESTAMP SINCE WE ALREADY SORT MESSAGES ACCORDING TO TIMESTAMP
I first search for duplicated lines in old log. If duplicates are found, check new log file to see if the line was already been redirected to it.
no_of_duplicated_lines=`grep "$line_to_find" "$1"|wc -l`
if [[ "$no_of_duplicated_lines" != @(1) ]]; then
matching_line_in_log_files=`grep "$line_to_find" "$2"`
if [ -z "$matching_line_in_log_files" ]; then
echo "$line" >> "$2"
fi
else
[STEP 6] IF NO. OF LINES = 1, REDIRECT THE LINE TO NEW LOG FILE
If only one line is found in old log and it is not a duplicate just redirect the line from old log to new log.
echo "$line" >> "$2"
fi
[STEP 7] SORT YOUR NEW LOG FILE IN TIME STAMP ORDER- RECENT TO OLDEST
open your new log and sort according to recent time and redirect to tmp file. Afterwards, redirect all sorted output from tmp file to new log. remove tmp file after process finished.
cat "$2" > tmp
sort -r tmp > "$2"
rm -f tmp 2> /dev/null
YOUR OUTPUT ACCORDING TO TIME ORDER
22:35:59 daemon DENIED: "Prog2" userd server92 (User/host not on INCLUDE list for feature.)
21:58:40 daemon DENIED: "Prog2" userd server04 (User/host not on INCLUDE list for feature.)
21:58:38 daemon DENIED: "Prog3" userb server97 (User/host not on INCLUDE list for feature.)
21:58:38 daemon DENIED: "Prog2" userb server97 (User/host not on INCLUDE list for feature.)
21:32:51 daemon DENIED: "Prog1" usera server39 (Licensed number of users already reached.)
21:32:48 daemon DENIED: "Prog1" usera server82 (Licensed number of users already reached.)