Sorting block of lines based on specified strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting block of lines based on specified strings
# 1  
Old 03-18-2013
Sorting block of lines based on specified strings

Below is the part of contents of a file

Code:
cat outputFile.txt
 
[LOW] :APP-MS-service_monitoring-118054-DiskFilling-S
Description=Disk is getting filled. 
If Condition=If Event/Log Level(ANY) and matching substring "DiskFilling" Log: xx.x.x..xx.: 4 days 1:33:54.42
Date and time=03-18-2013 04:45:01 AM
 
[MED] -144:APP-MS-lib_license_common-150068-licenseFileFingerprintMismatch-S
Description=Appliance fingerprint did not match with the enterprise license file. If this is not corrected soon, the current enterprise license will be de-activated in the near. 
If Condition=If Event/Log Level(ANY) and matching substring "licenseFileFingerprintMismatch" Log: xx.x.x..xx. : 4 days 1:23:54.84
Date and time=03-18-2013 04:36:07 AM
 
[HIGH] -144:APP-VA-va_ctiadapter-110005-VA_CTI_RECORDING_LICENSE_FAILURE-S
Description=Recording Licenses are not available for user Id= {0} ,for number of {1} recording mixes. 
If Condition=If Event/Log Level(ANY) and matching substring "VA_CTI_RECORDING_LICENSE_" Log: xx.x.x..xx. : 4 days 1:23:46.39
Date and time=03-18-2013 04:35:58 AM
 
[LOW] 144:APP-MS-service_monitoring-118054-DiskFilling-S
Description=Disk is getting filled. 
If Condition=If Event/Log Level(ANY) and matching substring "DiskFilling" Log: xx.x.x..xx.: 1:10:10.69 1.3.6.1.4.1.1453.4.9.1.3.0.1
Date and time=03-18-2013 04:15:01 AM
 
[HIGH] 140 CCM-FileServer Mount /home:CCM-Platform-106005-FileServerUsage95-P
Description=File Server usage exceeds 95%.  Resolution hint: clean up disk, upgrade disk. 
If Condition=If Use Percent > 85.0% (actual value 100.0%)
Date and time=03-18-2013 04:10:00 AM



here the blocks are HIGH MED and LOW.... but the file is unsorted.. i want to sort them based on high med and low in decreasing order of severity and take as less time as possible for this sorting operation... so basically my final output file should be as below

Code:
cat outputFile.txt
[HIGH] -144:APP-VA-va_ctiadapter-110005-VA_CTI_RECORDING_LICENSE_FAILURE-S
Description=Recording Licenses are not available for user Id= {0} ,for number of {1} recording mixes. 
If Condition=If Event/Log Level(ANY) and matching substring "VA_CTI_RECORDING_LICENSE_" Log: xx.x.x..xx. : 4 days 1:23:46.39
Date and time=03-18-2013 04:35:58 AM
 
[HIGH] 140 CCM-FileServer Mount /home:CCM-Platform-106005-FileServerUsage95-P
Description=File Server usage exceeds 95%.  Resolution hint: clean up disk, upgrade disk. 
If Condition=If Use Percent > 85.0% (actual value 100.0%)
Date and time=03-18-2013 04:10:00 AM
 
[MED] 144:APP-MS-lib_license_common-150068-licenseFileFingerprintMismatch-S
Description=Appliance fingerprint did not match with the enterprise license file. If this is not corrected soon, the current enterprise license will be de-activated in the near. 
If Condition=If Event/Log Level(ANY) and matching substring "licenseFileFingerprintMismatch" Log: xx.x.x..xx. : 4 days 1:23:54.84
Date and time=03-18-2013 04:36:07 AM
 
[LOW] 144:APP-MS-service_monitoring-118054-DiskFilling-S
Description=Disk is getting filled. 
If Condition=If Event/Log Level(ANY) and matching substring "DiskFilling" Log: xx.x.x..xx.: 4 days 1:33:54.42
Date and time=03-18-2013 04:45:01 AM
 
[LOW] 144:APP-MS-service_monitoring-118054-DiskFilling-S
Description=Disk is getting filled. 
If Condition=If Event/Log Level(ANY) and matching substring "DiskFilling" Log: xx.x.x..xx.: 1:10:10.69 1.3.6.1.4.1.1453.4.9.1.3.0.1
Date and time=03-18-2013 04:15:01 AM


any help is appreciated.. thanks

---------- Post updated at 04:30 PM ---------- Previous update was at 04:01 PM ----------

got it :-)

from this link
HTML Code:
https://www.unix.com/shell-programming-scripting/216296-remove-part-file-based-identifiers.html

i did some change and performed below ops


Code:
awk '/\[HIGH\]/' RS="\n *\n" ORS="\n\n" alertsOutput.txt >alertsOutputHIGH.txt
 
 
awk '/\[MED\]/' RS="\n *\n" ORS="\n\n" alertsOutput.txt >alertsOutputMED.txt
 
awk '/\[LOW\]/' RS="\n *\n" ORS="\n\n" alertsOutput.txt >alertsOutputLOW.txt


and finally append them in order :-) ...

Last edited by vivek d r; 03-21-2013 at 01:16 AM..
# 2  
Old 03-18-2013
Hi.

The msort utility can do tasks such as sort by blocks, and with custom collating sequences:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate block sort with custom ordering.
# See: http://freecode.com/projects/msort

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, default ordering:"
msort -q -j -b --position 1,1 --comparison-type l $FILE

pl " Custom ordering file:"
head data2

pl " Results, custom ordering:"
msort -q -j -b --position 1,1 -u n --sort-order data2 --comparison-type l $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
[low] blah2
This is a low block.

[high] blah1
This is a high block.

[med] blah3 
This is a medium block.

-----
 Results, default ordering:
[high] blah1
This is a high block.

[low] blah2
This is a low block.

[med] blah3 
This is a medium block.


-----
 Custom ordering file:
high
med
low

-----
 Results, custom ordering:
[high] blah1
This is a high block.

[med] blah3 
This is a medium block.

[low] blah2
This is a low block.

The msort command can be found in some repositories, such as FreeBSD, Debian, Fedora. If not available directly to you, you can try obtaining it from the site noted in the comments of the demonstration script above, or MSORT

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 3  
Old 03-18-2013
thanks drl.. i will go throught this msort... looks interesting :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output block of lines in a file based on grep result

Hi I would appreciate your help with this. I have a output file from a command. It is broken based on initial of the users. Exmaple of iitials MN & SS. Under each section there is information pertaining to the user however each section can have different number of lines. MY challenge is to ... (5 Replies)
Discussion started by: mnassiri
5 Replies

2. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

3. Shell Programming and Scripting

Sorting alphanumeric strings without a pattern

Good evening to all!! I'm facing this problem: I saved in a txt a list of files name (one txt for every folder): hello0.jpg hello1.jpg hello10.jpg hello11.jpg hello12.jpg hello13.jpg hello14.jpg hello15.jpg hello16.jpg hello17.jpg hello18.jpg hello19.jpg hello2.jpg hello20.jpg... (32 Replies)
Discussion started by: silver18
32 Replies

4. Shell Programming and Scripting

Sorting lines based on keywords for MySQL script

the thing which i require is very very complex.. i tried hard to find the solution but couldnt.. the thing i need to achieve is say i have a file cat delta.sql CREATE VIEW Austin Etc etc . . . CREATE VIEW Barabara AS SELECT blah blah blah FROM Austin z, Cluster s, Instance i WHERE... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

6. Shell Programming and Scripting

sorting based on alternative lines

Hello, I have a file with multiple entries. @SFGF-GA2-1_58:5:36:11009:999#0/1 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +SFGF-GA2-1_58:5:36:11009:999#0/1 ################################################################################... (10 Replies)
Discussion started by: Diya123
10 Replies

7. Programming

Sorting a vector of strings into numerical order.

I have a vector of strings that contain a list of channels like this: 101,99,22HD,432,300HD I have tried using the sort routine like this: sort(mychans.begin(),mychans.end()); For some reason my channels are not being sorted at all. I was hoping someone might have some input that might... (2 Replies)
Discussion started by: sepoto
2 Replies

8. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

9. UNIX for Dummies Questions & Answers

Delete lines with duplicate strings based on date

Hey all, a relative bash/script newbie trying solve a problem. I've got a text file with lots of lines that I've been able to clean up and format with awk/sed/cut, but now I'd like to remove the lines with duplicate usernames based on time stamp. Here's what the data looks like 2007-11-03... (3 Replies)
Discussion started by: mattv
3 Replies

10. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies
Login or Register to Ask a Question