ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a system file, which contains the system event notification information details cumulatively. And the file contains the information in every event begin with
>------------ Event Monitoring Service Event Notification ------------<
and end like
>---------- End Event Monitoring Service Event Notification ----------<
I need to write a script which will read the last event of file which are begin in
>------------ Event Monitoring Service Event Notification ------------<
and end in
>---------- End Event Monitoring Service Event Notification ----------<
how can I read the last event of file not using "tail" but matching between the last "Event Monitoring Service Event Notification" and "End Event Monitoring Service Event Notification".
awk '/- Event/{delete a}
/- Event/,/End Event/{a[count++]=$0}
END{for (i in a) print a[i]}' file
Using your post as input file:
Code:
awk '/- Event/{delete a}
> /- Event/,/End Event/{a[count++]=$0}
> END{for (i in a) print a[i]}' file
>------------ Event Monitoring Service Event Notification ------------<
and end in
>---------- End Event Monitoring Service Event Notification ----------<
Nice.
colucix, you are going to convince me I need to learn to use awk properly.
We agreed on the (form of) the solution, but I'd never have done it so succinctly.
I need the last description of the file after ">------------ Event Monitoring Service Event Notification ------------<" and up to ">---------- End Event Monitoring Service Event Notification ----------<"
between the above quotations I need the inside description.
I need the last description of the file after ">------------ Event Monitoring Service Event Notification ------------<" and up to ">---------- End Event Monitoring Service Event Notification ----------<"
between the above quotations I need the inside description.
Just don't print the first and the last element of the array:
colucix Just pasting the code for my snippet library I tried both but the last one (with length()) I get:
awk: cmd. line:2: (FILENAME=file FNR=11) fatal: attempt to use array `a' in a scalar context
Which version of awk do you have? Maybe the length function is limited to retrieving the length of a string and length of arrays is not supported. Take in mind that it is a GNU awk extension and that older version of gawk does not support this feature. It works with my version 3.1.5.
for an example i'm giving you the text file sample. This is the last entry of my file
>------------ Event Monitoring Service Event Notification ------------<
Notification Time: Fri Jun 19 18:21:35 2009
blbiltst sent Event Monitor notification information:
/adapters/events/TL_adapter/0_6_1_0 is >= 1.
Its current value is INFORMATION(1).
Event data from monitor:
Event Time..........: Fri Jun 19 18:21:34 2009
Severity............: INFORMATION
Monitor.............: dm_TL_adapter
Event #.............: 18
System..............: server
Summary:
Adapter at hardware path 0/6/1/0 : Received an interrupt indicating that a
primitive was transmitted
>---------- End Event Monitoring Service Event Notification ----------<
i want to get the last description quoted between ">------------ Event Monitoring Service Event Notification ------------<" and ">---------- End Event Monitoring Service Event Notification ----------<"
But i never know which description will come last. I actually need this.
What does not work? Given your example description (I put in two descriptions with a slight difference inside them and some additional lines to be excluded), here is what I get:
Code:
$ cat testfile
line excluded
line excluded
>------------ Event Monitoring Service Event Notification ------------<
Notification Time: Fri Jun 19 18:21:35 2009
blbiltst sent Event Monitor notification information:
/adapters/events/TL_adapter/0_6_1_0 is >= 1.
line excluded
Its current value is INFORMATION(1).
Event data from monitor:
Event Time..........: Fri Jun 19 18:21:34 2009
Severity............: INFORMATION
Monitor.............: dm_TL_adapter
Event #.............: 18
System..............: server
Summary:
Adapter at hardware path 0/6/1/0 : Received an interrupt indicating that a
primitive was transmitted
>---------- End Event Monitoring Service Event Notification ----------<
line excluded
line excluded
>------------ Event Monitoring Service Event Notification ------------<
Notification Time: Fri Jun 19 18:21:35 2009
blbiltst sent Event Monitor notification information:
/adapters/events/TL_adapter/0_6_1_0 is >= 1.
line included
Its current value is INFORMATION(1).
Event data from monitor:
Event Time..........: Fri Jun 19 18:21:34 2009
Severity............: INFORMATION
Monitor.............: dm_TL_adapter
Event #.............: 18
System..............: server
Summary:
Adapter at hardware path 0/6/1/0 : Received an interrupt indicating that a
primitive was transmitted
>---------- End Event Monitoring Service Event Notification ----------<
line excluded
line excluded
$ awk '/- Event/{delete a; count=0}
/- Event/,/End Event/{a[++count]=$0}
END{for (i=1; i<=NR; i++) if (i in a) print a[i]}' testfile
>------------ Event Monitoring Service Event Notification ------------<
Notification Time: Fri Jun 19 18:21:35 2009
blbiltst sent Event Monitor notification information:
/adapters/events/TL_adapter/0_6_1_0 is >= 1.
line included
Its current value is INFORMATION(1).
Event data from monitor:
Event Time..........: Fri Jun 19 18:21:34 2009
Severity............: INFORMATION
Monitor.............: dm_TL_adapter
Event #.............: 18
System..............: server
Summary:
Adapter at hardware path 0/6/1/0 : Received an interrupt indicating that a
primitive was transmitted
>---------- End Event Monitoring Service Event Notification ----------<
If you don't want the header lines:
Code:
$ awk '/- Event/{delete a; count=0}
/- Event/,/End Event/{a[++count]=$0}
END{for (i=3; i<=NR; i++) if (i in a) print a[i-1]}' testfile
Notification Time: Fri Jun 19 18:21:35 2009
blbiltst sent Event Monitor notification information:
/adapters/events/TL_adapter/0_6_1_0 is >= 1.
line included
Its current value is INFORMATION(1).
Event data from monitor:
Event Time..........: Fri Jun 19 18:21:34 2009
Severity............: INFORMATION
Monitor.............: dm_TL_adapter
Event #.............: 18
System..............: server
Summary:
Adapter at hardware path 0/6/1/0 : Received an interrupt indicating that a
primitive was transmitted
If the above does not match your requirement, can you post an example of the desired output? Using CODE tags, please.
When I run the command, it gives me the following error
awk '/- Event/{delete a; count=0}
> /- Event/,/End Event/{a[++count]=$0}
> END{for (i=3; i<=NR; i++) if (i in a) print a[i-1]}' testfile
syntax error The source line is 1.
The error context is
/- Event/{delete >>> a; <<<
awk: The statement cannot be correctly parsed.
The source line is 1.
So you're not using the GNU awk, aren't you? Indeed the ability to delete a whole array using
Code:
delete array
is a gawk extension. The following code should work for you:
Code:
nawk '/- Event/{for (i in a) delete a[i]; count=0} \
/- Event/,/End Event/{a[++count]=$0} \
END{for (i=3; i<=NR; i++) if (i in a) print a[i-1]}' testfile
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.