LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to sort output at latest entries without disturbing the previous entries record (http://www.linuxquestions.org/questions/programming-9/how-to-sort-output-at-latest-entries-without-disturbing-the-previous-entries-record-620122/)

nabmufti 02-10-2008 12:21 PM

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

angrybanana 02-10-2008 02:04 PM

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

nabmufti 02-11-2008 04:05 AM

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

osor 02-11-2008 12:01 PM

Quote:

Originally Posted by nabmufti (Post 3053225)
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 (Post 3053225)
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.

angrybanana 02-11-2008 11:36 PM

Quote:

Originally Posted by nabmufti (Post 3053225)
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


All times are GMT -5. The time now is 04:54 AM.