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.
I want to write a shell script so that it selects a pattern from a file and copies only that part. In this file I have to search for "Errors" pattern. If this is present I must check if it contains "ORA" in the subsequent line. If this is present then I have to copy from a line previous to the "Errors" line till the next date.
How can this be accomplished. Can anyone Help?
The file is as follows.
Fri Aug 31 13:08:43 2007
Errors in file /home/oracle10/admin/EAIDB/bdump/eaidb_psp0_29199.trc:
ORA-27300: OS system dependent operation:fork failed with status: 11
ORA-27301: OS failure message: Resource temporarily unavailable
ORA-27302: failure occurred at: skgpspawn5
Wed Sep 5 16:59:25 2007
Thread 1 advanced to log sequence 5437
Current log# 1 seq# 5437 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo01.log
Wed Sep 5 17:08:09 2007
Thread 1 advanced to log sequence 5438
Current log# 4 seq# 5438 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo4.log
Wed Sep 5 17:13:55 2007
Thread 1 advanced to log sequence 5439
Current log# 2 seq# 5439 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo02.log
Wed Sep 5 17:17:07 2007
Thread 1 advanced to log sequence 5440
Current log# 3 seq# 5440 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo03.log
Wed Sep 5 17:17:29 2007
Thread 1 advanced to log sequence 5441
Current log# 5 seq# 5441 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo5.log
Wed Sep 5 17:19:29 2007
Thread 1 advanced to log sequence 5442
Current log# 1 seq# 5442 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo01.log
Wed Sep 5 17:28:32 2007
Thread 1 advanced to log sequence 5443
Wed Sep 5 23:32:42 2007
Errors in file /home/oracle10/admin/EAIDB/bdump/eaidb_j000_17397.trc:
ORA-12012: error on auto execute of job 8874
ORA-20000: ORA-20000: Content of the tablespace specified is not permanent or tablespace name is invalid
ORA-06512: at "SYS.PRVT_ADVISOR", line 1624
ORA-06512: at "SYS.DBMS_ADVISOR", line 186
ORA-06512: at "SYS.DBMS_SPACE", line 1338
ORA-06512: at "SYS.DBMS_SPACE", line 1554
Mon Sep 3 16:01:49 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_10177.trc:
ORA-07445: exception encountered: core dump [skgfqio()+112] [SIGBUS] [Invalid address alignment] [0x9FFFFFFFFFF9FF50] [] []
Mon Sep 3 16:01:51 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_10177.trc:
ORA-07445: exception encountered: core dump [$cold_kghfrf()+2288] [SIGSEGV] [Address not mapped to object] [0x000010026] [] [
]
ORA-04030: out of process memory when trying to allocate 753136 bytes (pga heap,kco buffer)
ORA-07445: exception encountered: core dump [skgfqio()+112] [SIGBUS] [Invalid address alignment] [0x9FFFFFFFFFF9FF50] [] []
Mon Sep 3 16:01:53 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_10177.trc:
ORA-07445: exception encountered: core dump [$cold_kghfrf()+2288] [SIGSEGV] [Address not mapped to object] [0x000010026] [] [
]
ORA-04030: out of process memory when trying to allocate 753136 bytes (pga heap,kco buffer)
ORA-07445: exception encountered: core dump [$cold_kghfrf()+2288] [SIGSEGV] [Address not mapped to object] [0x000010026] [] [
]
ORA-04030: out of process memory when trying to allocate 753136 bytes (pga heap,kco buffer)
ORA-07445: exception encountered: core dump [skgfqio()+112] [SIGBUS] [Invalid address alignment] [0x9FFFFFFFFFF9FF50] [] []
Mon Sep 3 16:02:39 2007
Errors in file /home/oracle10/admin/EAIDB/bdump/eaidb_psp0_29199.trc:
ORA-27300: OS system dependent operation:fork failed with status: 11
ORA-27301: OS failure message: Resource temporarily unavailable
ORA-27302: failure occurred at: skgpspawn5
Mon Sep 3 16:02:40 2007
Hello Is my Question too dificult to understand or solve
LQ is a volunteer-driven community effort relying on its members to supply answers if and when they want to. If you need on-demand 24/7 coverage, please consider hiring a local Linux guru, else just be patient.
And no, your question isn't difficult but you wrote
Quote:
Originally Posted by sagarbsa
I want to write a shell script
which means:
A) you want to write the script. In that case you do the research (http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html, http://www.tldp.org/LDP/Bash-Beginne...tml/index.html), post what script you've got and we help you after that. This is the preferred way since you actually learn something.
B) you require a fellow LQ member to write the script for you (needs only a "while" loop and two tests). In that case you rephrase your demand as a polite question and exercise patience.
If you used sed, it will probably be a lot easier coming to a solution if you inserted an empty line before each date.
Code:
$ sed -r '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/i\
' logfile | ...
That would allow a range like this:
$ sed -rn '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/,/^$/{
...
}
Then you could add each line to the working buffer inside the range and when you reach /^$/, test for the pattern '\nORA'. If it is found, print out the working buffer.
You might want to test this out:
Code:
$ sed -r '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/i\
' junk | sed -rn '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/,/^$/{
/^$/!H
/^$/{
x
/ORA/p
}
}'
Fri Aug 31 13:08:43 2007
Errors in file /home/oracle10/admin/EAIDB/bdump/eaidb_psp0_29199.trc:
ORA-27300: OS system dependent operation:fork failed with status: 11
ORA-27301: OS failure message: Resource temporarily unavailable
ORA-27302: failure occurred at: skgpspawn5
Wed Sep 5 23:32:42 2007
Errors in file /home/oracle10/admin/EAIDB/bdump/eaidb_j000_17397.trc:
ORA-12012: error on auto execute of job 8874
ORA-20000: ORA-20000: Content of the tablespace specified is not permanent or tablespace name is invalid
ORA-06512: at "SYS.PRVT_ADVISOR", line 1624
ORA-06512: at "SYS.DBMS_ADVISOR", line 186
ORA-06512: at "SYS.DBMS_SPACE", line 1338
ORA-06512: at "SYS.DBMS_SPACE", line 1554
Mon Sep 3 16:01:49 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_10177.trc:
ORA-07445: exception encountered: core dump [skgfqio()+112] [SIGBUS] [Invalid address alignment] [0x9FFFFFFFFFF9FF50] [] []
Mon Sep 3 16:01:51 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_10177.trc:
ORA-07445: exception encountered: core dump [$cold_kghfrf()+2288] [SIGSEGV] [Address not mapped to object] [0x000010026] [] [
]
ORA-04030: out of process memory when trying to allocate 753136 bytes (pga heap,kco buffer)
ORA-07445: exception encountered: core dump [skgfqio()+112] [SIGBUS] [Invalid address alignment] [0x9FFFFFFFFFF9FF50] [] []
Mon Sep 3 16:01:53 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_10177.trc:
ORA-07445: exception encountered: core dump [$cold_kghfrf()+2288] [SIGSEGV] [Address not mapped to object] [0x000010026] [] [
]
ORA-04030: out of process memory when trying to allocate 753136 bytes (pga heap,kco buffer)
ORA-07445: exception encountered: core dump [$cold_kghfrf()+2288] [SIGSEGV] [Address not mapped to object] [0x000010026] [] [
]
ORA-04030: out of process memory when trying to allocate 753136 bytes (pga heap,kco buffer)
ORA-07445: exception encountered: core dump [skgfqio()+112] [SIGBUS] [Invalid address alignment] [0x9FFFFFFFFFF9FF50] [] []
It needs some debugging so that the last entry isn't missed. The input file I used didn't end in a empty line so that range wasn't counted.
Here I'll cheat:
Code:
echo >>junk
$ sed -r '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/i\
' junk | sed -rn '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/,/^$/{
/^$/!H
/^$/{
x
/ORA/p
}
}'
Now the log ends in an empty space and the last entry output.
Thanks JSCHIWAL. But I could not understand the program can u help me out with it? Also it should print only if both Errors and ORA patterns are found assuming that in the file there are line which has only ORA patterns also. How do I modify the same for that. The file is as follows.
Mon Jul 16 20:28:01 2007
Thread 1 advanced to log sequence 2330
Current log# 3 seq# 2330 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo03.log
Mon Jul 16 20:33:16 2007
Thread 1 advanced to log sequence 2331
Current log# 5 seq# 2331 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo5.log
Mon Jul 16 20:38:36 2007
Thread 1 advanced to log sequence 2332
Current log# 1 seq# 2332 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo01.log
Mon Jul 16 20:43:36 2007
Thread 1 advanced to log sequence 2333
Current log# 4 seq# 2333 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo4.log
Mon Jul 16 20:49:00 2007
Thread 1 advanced to log sequence 2334
Current log# 2 seq# 2334 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo02.log
Mon Jul 16 20:54:18 2007
Thread 1 advanced to log sequence 2335
Current log# 3 seq# 2335 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo03.log
Mon Jul 16 20:59:32 2007
Thread 1 advanced to log sequence 2336
Current log# 5 seq# 2336 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo5.log
Mon Jul 16 21:04:50 2007
Thread 1 advanced to log sequence 2337
Current log# 1 seq# 2337 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo01.log
Mon Jul 16 21:09:52 2007
Thread 1 advanced to log sequence 2338
Current log# 4 seq# 2338 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo4.log
Mon Jul 16 21:15:13 2007
Thread 1 advanced to log sequence 2339
Current log# 2 seq# 2339 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo02.log
Mon Jul 16 21:20:32 2007
Thread 1 advanced to log sequence 2340
Current log# 3 seq# 2340 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo03.log
Mon Jul 16 21:25:48 2007
Thread 1 advanced to log sequence 2341
Current log# 5 seq# 2341 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo5.log
Mon Jul 16 21:31:05 2007
Thread 1 advanced to log sequence 2342
Current log# 1 seq# 2342 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo01.log
Mon Jul 23 22:00:30 2007
Errors in file /home/oracle10/admin/EAIDB/bdump/eaidb_j000_19631.trc:
ORA-12012: error on auto execute of job 8874
ORA-13607: The specified task or object ORA-13607: The specified task or object SYS_AUTO_SPCADV_2930162372007 already exists
ORA-06512: at "SYS.PRVT_ADVISOR", line 3902
ORA-06512: at "SYS.DBMS_ADVISOR", line 102
ORA-06512: at "SYS.DBMS_SPACE", line 1450
ORA-06512: at "SYS.DBMS_SPACE", line 1554
already exists
Mon Jul 23 23:19:07 2007
Thread 1 advanced to log sequence 2364
Current log# 2 seq# 2364 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo02.log
Tue Jul 24 21:31:59 2007
Thread 1 advanced to log sequence 2365
Current log# 3 seq# 2365 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo03.log
Wed Jul 25 16:26:37 2007
Thread 1 advanced to log sequence 2366
Current log# 5 seq# 2366 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo5.log
Thu Jul 26 11:15:51 2007
Shutting down instance: further logons disabled
Fri Aug 10 16:13:16 2007
ORA-3297 signalled during: ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 5000M...
Fri Aug 10 16:13:22 2007
ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 6000M
ORA-3297 signalled during: ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 6000M...
Fri Aug 10 16:13:28 2007
ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 7000M
Fri Aug 10 16:13:28 2007
ORA-3297 signalled during: ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 7000M...
Fri Aug 10 16:13:34 2007
ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 8000M
ORA-3297 signalled during: ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 8000M...
Fri Aug 10 16:13:40 2007
ALTER DATABASE DATAFILE '/Softwares/oracle10/oradata/EAIDB/eaidata01b.dbf' RESIZE 9000M
Fri Aug 10 19:59:09 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_17192.trc:
ORA-07445: exception encountered: core dump [kcsgrsn()+400] [SIGSEGV] [Invalid permissions for mapped object] [0x0000006EC] [
] []
ORA-03113: end-of-file on communication channel
ORA-03113: end-of-file on communication channel
Fri Aug 10 19:59:10 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_17192.trc:
ORA-07445: exception encountered: core dump [kcsgrsn()+400] [SIGSEGV] [Invalid permissions for mapped object] [0x0000006EC] [
] []
ORA-07445: exception encountered: core dump [kcsgrsn()+400] [SIGSEGV] [Invalid permissions for mapped object] [0x0000006EC] [
] []
ORA-03113: end-of-file on communication channel
ORA-03113: end-of-file on communication channel
Fri Aug 10 20:05:04 2007
Thread 1 advanced to log sequence 2694
Current log# 2 seq# 2694 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo02.log
Fri Aug 10 20:05:24 2007
Thread 1 advanced to log sequence 2695
Current log# 3 seq# 2695 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo03.log
Fri Aug 10 20:05:42 2007
Thread 1 advanced to log sequence 2696
Current log# 5 seq# 2696 mem# 0: /Softwares/oracle10/oradata/EAIDB/redo5.log
Mon Aug 20 22:43:04 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:04 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:04 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:04 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:04 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:04 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:04 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:05 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:05 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:05 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:05 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:05 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Mon Aug 20 22:43:05 2007
ORA-1654: unable to extend index EAIMGR.SYS_C0010530 by 1024 in tablespace TCS_SLV_DATA01
Last edited by sagarbsa; 10-03-2007 at 10:31 PM.
Reason: Incomplete Data
Makes '\n' the field separator (each line is a field)
Code:
/Errors/{...}
checks if the term "Errors" is in the record (the block between each Mon-Sun found) If it's found it runs the code.
Code:
{for(i=1;i<=NF;i++){
if ($i~/^ORA/) {
print rt$0;rt=RT;next}
loops from 1 to NF (number of fields). Basically it's scanning all fields, checks if the field starts with ORA. if it does it prints the the whole record and goes to the "next" one.
Code:
rt=RT
I did this because RT= record terminator, so it would give me the name that matched the end of the record, not the start. So I just copy the RT to rt as i scan, and insert rt (the one from the previous record) before my printout.
I hope it makes sense enough for you to figure out.
PS:
You can add something like
Code:
ORS='-------------\n'
if it'll help you with readability of the output.
here's the output using your second sample as a source:
Code:
awk -F '\n' '/Errors/{for(i=1;i<=NF;i++){
if ($i~/^ORA/) {
print rt$0;rt=RT;next}
}}{rt=RT}' RS='(Mon|Tue|Wed|Thu|Fri|Sat|Sun)' ORS='-------------\n' 'file'
Mon Jul 23 22:00:30 2007
Errors in file /home/oracle10/admin/EAIDB/bdump/eaidb_j000_19631.trc:
ORA-12012: error on auto execute of job 8874
ORA-13607: The specified task or object ORA-13607: The specified task or object SYS_AUTO_SPCADV_2930162372007 already exists
ORA-06512: at "SYS.PRVT_ADVISOR", line 3902
ORA-06512: at "SYS.DBMS_ADVISOR", line 102
ORA-06512: at "SYS.DBMS_SPACE", line 1450
ORA-06512: at "SYS.DBMS_SPACE", line 1554
already exists
-------------
Fri Aug 10 19:59:09 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_17192.trc:
ORA-07445: exception encountered: core dump [kcsgrsn()+400] [SIGSEGV] [Invalid permissions for mapped object] [0x0000006EC] [
] []
ORA-03113: end-of-file on communication channel
ORA-03113: end-of-file on communication channel
-------------
Fri Aug 10 19:59:10 2007
Errors in file /home/oracle10/admin/EAIDB/udump/eaidb_ora_17192.trc:
ORA-07445: exception encountered: core dump [kcsgrsn()+400] [SIGSEGV] [Invalid permissions for mapped object] [0x0000006EC] [
] []
ORA-07445: exception encountered: core dump [kcsgrsn()+400] [SIGSEGV] [Invalid permissions for mapped object] [0x0000006EC] [
] []
ORA-03113: end-of-file on communication channel
ORA-03113: end-of-file on communication channel
-------------
Edit: Didn't realize that the line immediately after Error starts with ORA. Anyways, here's a non 2am 1/2 asleep version
[quote=sagarbsa;2912509]Thanks JSCHIWAL. But I could not understand the program can u help me out with it?
Code:
$ sed -r '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/i\ # Matches lines starting with a date, and inserts a blank line before it. This makes selecting a range easier.
' junk | sed -rn '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/,/^$/{ # Selects a single section at a time. Inside a bracket, you can have pattern tests for lines in between.
/^$/!H # If this isn't a blank line (end of the range), then add this line to the Hold buffer.
/^$/{ # At the end of the pattern, so check if the patter 'ORA' exists. If so, yank back the Hold buffer and print it out.
x # exchange the current working buffer (blank) with the Hold buffer. This both recalls the lines from the hold buffer and clears the hold buffer.
/ORA/p # print out the lines.
}
}'
I don't see any lines in the sample set that contain an error.
Since we used the "H" command, the working register contains multiple lines, so the pattern
from both lines of input are present and can be tested for.
Code:
echo >>junk
$ sed -r '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/i\
' junk | sed -rn '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/,/^$/{
/^$/!H
/^$/{
x
/Error.*\nORA/p
}
}'
I.e. my awk is actually gawk.
It is likely that HP-UX is running something like nawk.
Why don't you can check the ver. on both machines & post back, please?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.