The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sort csv file mozart Shell Programming and Scripting 6 02-11-2008 03:49 AM
add columns from file to another and sort kamel.seg Shell Programming and Scripting 12 12-12-2007 02:39 PM
How to Sort a file for given situation? vishalpatel03 Shell Programming and Scripting 4 11-26-2007 10:53 AM
Sort and compare file sabercats Shell Programming and Scripting 3 03-27-2006 03:05 PM
how to sort a file tao UNIX for Dummies Questions & Answers 2 03-24-2002 11:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2008
nabmufti nabmufti is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 15
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 (permalink)  
Old 02-13-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 02-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Is each record separated by a newline?? I can't tell, because sometimes the records have two newlines.
  #4 (permalink)  
Old 02-14-2008
nabmufti nabmufti is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 15
Quote:
Originally Posted by otheus View Post
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 (permalink)  
Old 02-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
a reply. see if you can use it.
  #6 (permalink)  
Old 02-14-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Use sort, not python.

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

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 (permalink)  
Old 02-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
Quote:
Originally Posted by otheus View Post
Dude, that's way overkill. I guess for python programmers, every problem is a nail?
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0