LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-10-2008, 01:21 PM   #1
nabmufti
LQ Newbie
 
Registered: Feb 2008
Posts: 13

Rep: Reputation: 0
Question how to sort output at latest entries without disturbing the previous entries record


hi all
i need to sort the output entry by entry
i am dealing with a log file of alkatel switch keeping three types of alarms i.e. minor (!) , major (!!) , and critical (!!!).
ineed to sort in order !!! then !! and ! in last
to make it more clear i give u an example that if i get 5 alarms in night and i open log file on next morning then !!!critcal alarm 'd be at first position then !! major alarm and ! minor alarm in the last,
my file sample is given below


!!! *A0628/303 /07-12-17/15 H 46/N=7506/TYP=COM/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 7/AGEO=ADABOSAN-B01
/TEXAL=SINGLE JUNCT/UT OOS/COMPL.INF:
/CN=2

!!! *A0628/303 /07-12-17/15 H 50/N=7507/TYP=COM/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 11/AGEO=QADIRPUR-B01
/TEXAL=SINGLE JUNCT/LINK DOWN /COMPL.INF:
/CN=2

!! *A0628/538 /07-12-17/12 H 52/N=7576/TYP=ICT/CAT=ID/EVENT=MAL
/NCEN=MULCT /AM =SMTA1/AGEO=S1-TR01-B03-A085-R000
/TEXAL=LCL MFM SYN/COMPL.INF:
/AF=URMA1
/ ICTRQ AGCA=S1-TR01-B03-A085-R133
/AMET=01-26-03
/AFLR=217-06/CRC=NACT

! *A0628/540 /07-12-17/15 H 58/N=7598/TYP=ICT/CAT=SI/EVENT=MAL
/NCEN=MULCT /AM =SMTA1/AGEO=S1-TR01-B03-A085-R000
/TEXAL=AIS/COMPL.INF:
/AF=URMA1
/ ICTRQ AGCA=S1-TR01-B03-A112-R065
/AMET=01-07-02
/AFLR=222-09/CRC=NACT



I am using the following code to extract output from log file ,

Code:
#!/bin/bash
sed  '/^!/i\
' log | sed -n -e '/^!!! /,/^$/w critical.log' -e '/^!! /,/^$/w major.log' -e'/^! /,/^$/w minor.log'
awk 'BEGIN {FS="/"; RS=""} { printf "%s/%s/%s/\n%s\n%s\n%s\n%s/%s\n\n", $1, $3, $4, $10, $12, $8, $14, $15}' minor.log 
exit 0
it gives me output as following extracting date and time, and other fields, i wanted

! *A0628 07-12-17/15 H 58/
NCEN=MULCT
AGEO=S1-TR01-B03-A085-R000
EVENT=MAL
TEXAL=AIS/COMPL.INF:

but the problem is this , it extracts output in three seperate log files i.e. minor.log , major.log ,critical.log
i want to see all out put from switch to a single log file in above sorted manner (as file sample above)
wait for reply soon
 
Old 02-10-2008, 03:04 PM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Do you want to just combine the files?
If so then this will do it.
Code:
cat critical.log major.log minor.log > output.log
 
Old 02-11-2008, 05:05 AM   #3
nabmufti
LQ Newbie
 
Registered: Feb 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Question i dont want it to combine

i realy dont want to combine these files
i want to sort the new alarms
i think it can be done using gawk instead awk, and i am trying to do so but still i am looking for the right command to do
i again try to make it clear
e.g. if i have 5 alarms unchecked in the file as latest enteries in log file then i want them in order as follows ,

!!! critical alarm at first
!! major alarm at second &
! minor alarm in last
when log fie is updated

i am usung the code i posted in thread above
hoping it will be more clear now
 
Old 02-11-2008, 01:01 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by nabmufti View Post
e.g. if i have 5 alarms unchecked in the file as latest enteries in log file then i want them in order as follows ,

!!! critical alarm at first
!! major alarm at second &
! minor alarm in last
when log fie is updated
Quote:
Originally Posted by nabmufti View Post
i realy dont want to combine these files
I fail to see the difference. If you follow angrybanana’s suggestion, what you end up with is one file, which contains all the critical alarms, followed by all the major alarms, followed by all the minor alarms.

You either use this directly or pipe the concatenated files as input to your awk command:
Code:
sed  '/^!/i\
' log | 
sed -n -e '/^!!! /,/^$/w critical.log' -e '/^!! /,/^$/w major.log' -e'/^! /,/^$/w minor.log'

cat {critical,major,minor}.log | 
awk 'BEGIN {FS="/"; RS=""} { printf "%s/%s/%s/\n%s\n%s\n%s\n%s/%s\n\n", $1, $3, $4, $10, $12, $8, $14, $15}'
You can, of course, delete the temporary files when you’re done with them.
 
Old 02-12-2008, 12:36 AM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by nabmufti View Post
i realy dont want to combine these files
i want to sort the new alarms
The reason I assumed it was fine to combine the files was because in another thread here you said you were happy with that solution. So I assumed you just wanted to build on that.

Anyways, this should get you started.
Code:
$ cat biglog
!! *A0628/538 /07-12-17/12 H 52/N=7576/TYP=ICT/CAT=ID/EVENT=MAL
/NCEN=MULCT /AM =SMTA1/AGEO=S1-TR01-B03-A085-R000
/TEXAL=LCL MFM SYN/COMPL.INF:
/AF=URMA1
/ ICTRQ AGCA=S1-TR01-B03-A085-R133
/AMET=01-26-03
/AFLR=217-06/CRC=NACT

!!! *A0628/303 /07-12-17/15 H 46/N=7506/TYP=COM/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 7/AGEO=ADABOSAN-B01
/TEXAL=SINGLE JUNCT/UT OOS/COMPL.INF:
/CN=2

! *A0628/540 /07-12-17/15 H 58/N=7598/TYP=ICT/CAT=SI/EVENT=MAL
/NCEN=MULCT /AM =SMTA1/AGEO=S1-TR01-B03-A085-R000
/TEXAL=AIS/COMPL.INF:
/AF=URMA1
/ ICTRQ AGCA=S1-TR01-B03-A112-R065
/AMET=01-07-02
/AFLR=222-09/CRC=NACT

!!! *A0628/303 /07-12-17/15 H 50/N=7507/TYP=COM/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 11/AGEO=QADIRPUR-B01
/TEXAL=SINGLE JUNCT/LINK DOWN /COMPL.INF:
/CN=2
This code should be clear enough for you to figure out the rest.
Code:
$ awk 'BEGIN{RS="";ORS="\n\n"} /^!!! /{critical[$0]} END{for (line in critical){print line}}' biglog 
!!! *A0628/303 /07-12-17/15 H 46/N=7506/TYP=COM/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 7/AGEO=ADABOSAN-B01
/TEXAL=SINGLE JUNCT/UT OOS/COMPL.INF:
/CN=2

!!! *A0628/303 /07-12-17/15 H 50/N=7507/TYP=COM/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 11/AGEO=QADIRPUR-B01
/TEXAL=SINGLE JUNCT/LINK DOWN /COMPL.INF:
/CN=2
Notice: This is after the file was processed with
Code:
sed  '/^!/i' biglog
You could probably do it all in awk and not need sed, but it's late and I'm too tired to mess with it.

Best of luck.

Edit:
Sigh... The whole sed+awk combination bugged me, so I couldn't go to sleep without changing it to be all in awk...
Code:
BEGIN{RS="!+ ";ORS="\n\n"}

NR>1 {
  sub("\n*$","")
  #if you don't understand this next section look up "ternary"
  #basically, it's an if/else statement, you can rewrite it using if/else
  (LRT=="!!! " ? critical[LRT$0]:
  LRT=="!! " ? major[LRT$0]:
  LRT=="! " ? minor[LRT$0]: "")
}
{LRT=RT}

END{
  for (line in critical){print line}
  for (line in major) {print line}
  for (line in minor) {print line}
}
Anyways write that to a file, lets say "foo.awk" for this example, then run it like this
Code:
awk -f foo.awk biglog

Last edited by angrybanana; 02-12-2008 at 01:13 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Courious "unknown" entries in "netstat" output Sheridan Linux - Networking 5 09-01-2009 10:09 AM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 03:57 PM
Entries in /etc/fstab kamransoomro84 Linux - General 6 05-05-2004 10:32 AM
HCL entries? synaptical LQ Suggestions & Feedback 19 02-18-2004 09:04 PM
What is the difference between these two entries? xailer Linux - Newbie 6 11-25-2003 07:59 AM


All times are GMT -5. The time now is 12:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration