LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-02-2007, 11:24 PM   #1
sagarbsa
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Rep: Reputation: 0
Shell Scripting


Hii,

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
 
Old 10-03-2007, 01:38 AM   #2
sagarbsa
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Hello Is my Question too dificult to understand or solve
 
Old 10-03-2007, 02:42 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,765
Blog Entries: 54

Rep: Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976
Quote:
Originally Posted by sagarbsa View Post
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 View Post
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.
 
Old 10-03-2007, 03:07 AM   #4
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 74
Moved: This thread is more suitable in the programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 10-03-2007, 03:29 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
You could probably do it using either sed or awk.

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.

Last edited by jschiwal; 10-03-2007 at 03:58 AM.
 
Old 10-03-2007, 11:12 PM   #6
sagarbsa
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Original Poster
Rep: Reputation: 0
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 11:31 PM. Reason: Incomplete Data
 
Old 10-04-2007, 01:51 AM   #7
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Here's an AWK solution.

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)' 'file'
explanation:
Code:
RS='(Mon|Tue|Wed|Thu|Fri|Sat|Sun)'
uses mon-sun as a record separator.
Code:
-F '\n'
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
Code:
awk -F'\n' '{for (i=1;i<=NF;i++)
    if ($i~/^Error/&&$(i+1)~/^ORA/) {print rt$0;break}}
    {rt=RT}' RS='(Mon|Tue|Wed|Thu|Fri|Sat|Sun)' 'file'

Last edited by angrybanana; 10-04-2007 at 09:11 AM.
 
Old 10-04-2007, 03:00 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
[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
                                                           }
                                                      }'

Last edited by jschiwal; 10-04-2007 at 03:07 AM.
 
Old 10-04-2007, 03:37 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
this does it...

Code:
#!/usr/bin/perl


while (<ARGV>) {
    if (/Errors in file/) {
        print;
        while (<ARGV>) {
            last unless /^ORA-/;
            print;
        }
    }
}

Last edited by bigearsbilly; 10-04-2007 at 03:48 AM.
 
Old 10-04-2007, 05:50 AM   #10
sagarbsa
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks a lot Dudes. Now the program is working fine. But Wanted to know what does "/^$/" and "x" implies.
 
Old 10-04-2007, 07:29 AM   #11
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Quote:
Originally Posted by sagarbsa View Post
Thanks a lot Dudes. Now the program is working fine. But Wanted to know what does "/^$/" and "x" implies.
/^$/ is the pattern that matches an empty line.
"x" exchanges the Hold buffer with pattern space. The pattern space is where lines are read into.
 
Old 10-08-2007, 01:12 AM   #12
sagarbsa
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks JSCHIWAL. The program is done now. Can you suggest some good awk and sed tutorial.
 
Old 10-10-2007, 02:46 AM   #14
sagarbsa
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Hii

I tried to run the above program inHP-UX in ksh. When I tried to run the awk program given above it gave the following Error.

awk: Input line Mon Jul 16 20:28:01 cannot be longer than 3,000 bytes.
The source line number is 4.

And for the sed program above I got the error stating
sed: illegal option -- r
sed: illegal option -- r
any solutions
 
Old 11-02-2007, 10:25 AM   #15
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Probably a different ver. of "awk".

On my box (SimplyMEPIS 6.0):
Code:
$ awk --version
GNU Awk 3.1.5
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?
 
  


Reply

Tags
scripting, shell, task


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
Shell Scripting greenberet Linux - Software 10 07-06-2006 11:21 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM
Shell scripting help rick_james Programming 3 05-20-2005 10:27 AM


All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration