sort certain patten ???not the whole file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort certain patten ???not the whole file?
# 1  
Old 02-13-2008
Question sort certain patten ???not the whole file?

hi all
i want help in sortng date in paragraphs within file ,
i want to ask as if there any option to sort a certain pattern of file not the rest of file.i.e the data of file become sorted with respect to date
i have a log file as follows


!! *A0628/081 /08-01-10/13 H 52/N=5524/TYP=INC/CAT=ID/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 13/AGEO=BAGRIAN -B01
/TEXAL=FIRE DETECT FAIL
!! *A0628/320 /08-01-15/14 H 33/N=7444/TYP=COM/CAT=ID/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 2/AGEO=CONTAIN2-B01
/TEXAL=FAULTY UNIT/COMPL.INF:
* #F0612/T11F14/NCEN=MULCT /08-01-30/11 H 41/NAM=ODCAB /TDA=0001
/N=7444/NIV=2/ENS=006/SENS=016-000-000/P='0018'H/CN=05
/EM: AFUR =URAD- 2


!! *A0628/538 /07-12-17/15 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/294 /07-12-17/15 H 46/N=7512/TYP=SRE/CAT=IM/EVENT=MAL
/NCEN=MULCT /OBJCT=PS/AGEO=CDC
/TEXAL=SP INACCESSIBLE/COMPL.INF:
/PS =00009999/TYR=RN


!! *A0628/361 /07-12-17/15 H 46/N=7513/TYP=COM/CAT=ID/EVENT=MAL
/NCEN=MULCT /AM =SMMA/AGEO=S1-TR02-B05-A109-R000
/TEXAL=SAI-HARDWARE/COMPL.INF:
/N=3485/ALARMA MATERIAL /R=00000/CU1A
/CB 200 BLOS
! *A0628/359 /07-12-17/15 H 46/N=7514/TYP=COM/CAT=SI/EVENT=MAL
/NCEN=MULCT /AM =SMMA/AGEO=S1-TR02-B05-A109-R000
/TEXAL=SAI-SOFTWARE/COMPL.INF:
/N=4543/ABNT X25 INDISPO. /R=00083/CU1A
/SNPA=A13381/PHYSLINE=LIGP13F1


!!! *A0628/080 /08-01-13/07 H 30/N=6540/TYP=INC/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 10/AGEO=RANGPUR -B01
/TEXAL=FIRE
!! *A0628/081 /07-12-29/16 H 22/N=5052/TYP=INC/CAT=ID/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 12/AGEO=KHOTYWAL-B01
/TEXAL=FIRE DETECT FAIL
!! *A0628/081 /08-01-10/13 H 52/N=5526/TYP=INC/CAT=ID/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 9/AGEO=BASTGLZR-B01
/TEXAL=FIRE DETECT FAIL


!!! *A0628/080 /08-01-10/13 H 52/N=5527/TYP=INC/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 7/AGEO=ADABOSAN-B01
/TEXAL=FIRE
!!! *A0628/087 /08-01-16/01 H 28/N=7648/TYP=CLI/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 10/AGEO=RANGPUR -B01

i want to extract the date , time , NCEN , EVENT , TAXAL & AGEO
i used the code following


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

this code does seperate the paragraphs follwing ! , !! , !!! (i.e. minor , major & critical alarms of the log file respectively ,in their respective log files i.e. minor.log ,major.log & critical.log )
and 'awk' gives me output as following

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

but i want the output to be sorted by date
(date pattern=/07-12-17/)
i tried to use the sort -k but i coldnt understand the right pattern to use it
or I have to sort this date pattern by loops ?
cant understand???

i tried to sort the date pattern by reg exp and to some extent i suceeded by i ca only compare two dated i couldnt get the logic to make the loop to check each date in file and then puxh the latest date entry on top each time
i used the code
Code:
#!/bin/bash
read date1
read date2
a=${date1:0:2}
b=${date2:0:2}
if $a -ge $b
then
echo $a
elif $a -le $b
then echo $b
else
echo "sorry"
fi
exit 0

plz do it if u can or tell me the right way to go ahead
# 2  
Old 02-13-2008
extract the fields first then sort them:
Code:
# extract date , time , NCEN , EVENT , TEXAL & AGEO, sort by date
# remove trailing blanks and then remove blank lines, then read, format line, print.
sed 's/[\t ]$//g' filename | grep -v '^$' | \
awk -F'/' ' {if(index($1, "!") == 1)
                { printf ("\n%s %s ", $3, $4)}
            else
                {   for(i=1; i<=NF; i++)
                	{
                	    if(index($i,"NCEN") == 1 || index($i,"EVENT")== 1 ||
                	       index($i,"TEXAL") == 1 || index($i,"AGEO") == 1 )
                	       { printf("%s ", $i) }
                	}
                }
             } END { printf("\n") } '  | sort

# 3  
Old 02-13-2008
Is each record separated by a newline?? I can't tell, because sometimes the records have two newlines.
# 4  
Old 02-14-2008
Quote:
Originally Posted by otheus
Is each record separated by a newline?? I can't tell, because sometimes the records have two newlines.
actually the line pattern is not regular but i extracted the output data
from file i.e. date , time ,NCEN , TAXAL , EVENT ,& AGEO
now i have the regular 1 line space in output

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

i used the following code to extract


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

now i need to sort it by date
but couldnt find the way
# 5  
Old 02-14-2008
a reply. see if you can use it.
# 6  
Old 02-14-2008
Use sort, not python.

Quote:
Originally Posted by ghostdog74
a reply. see if you can use it.
Dude, that's way overkill. I guess for python programmers, every problem is a nail? Smilie

nabmufti,

The normal sort routine will work. It looks like the date field is the third one within slashes, correct? If so, we can just do this:

sort -t / -k 3,4

Pipe the output of your script into this sort, and that should be it! Now, this works because the date is in proper lexical order (YY-MM-DD) and doesn't require individual sorting of the number space. If you had DD-MM-YY, it would be a tad bit trickier.

If you have multiple events on the same day, an you want to sort on another field, you can add a -k parameter:

sort -t / -k 3,4 -k 2,3

That will sort on the second /-separated field.
# 7  
Old 02-14-2008
Quote:
Originally Posted by otheus
Dude, that's way overkill. I guess for python programmers, every problem is a nail? Smilie
depends on how you look at it.
using that many a combination of sed's, grep's , awk's and sort, IMO, is the overkill.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

2. AIX

Sort file

as friends can only Sort this file with awk from low to high from position 3 to 9 bone this: 0003344 A70003344prueba1 A70003347prueba1 A70003345prueba1 A70003349prueba1 A70003343prueba1 A70003342prueba1 (4 Replies)
Discussion started by: tricampeon81
4 Replies

3. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

5. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

6. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

7. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

8. Shell Programming and Scripting

q's on file sort

I have a file below which has a list of users and roles with each row having unique combination of users and roles. USER1 ROLE1 USER1 ROLE2 USER2 USER3 ROLE1 USER3 ROLE2 USER3 ROLE3 USER4 ROLE2 .... .... I am trying to create a script which sorts the above file to have all the... (4 Replies)
Discussion started by: stevefox
4 Replies

9. Shell Programming and Scripting

Need to sort file

Hi, I have a file with the list of RPMs in the following format unix-abc-bin-1.27.1-006901 unix-abc-cfg-1.27.1-006901 unix-xyz-bin-1.27.1-006901 unix-abc-bin-1.27.2-006902 unix-xyz-bin-1.27.2-006902 unix-xyz-img-1.27.2-006902 I need the output as shown below unix-abc-bin-1.27.2-006902... (4 Replies)
Discussion started by: learnerlearner
4 Replies

10. UNIX for Dummies Questions & Answers

how to sort a file

Hi, i want to sort a file by column 2, 3, 10 and 4(in this order), can anybody help me? thanks a lot. (2 Replies)
Discussion started by: tao
2 Replies
Login or Register to Ask a Question