Find and Copy string from text file in Korn Shell script KSH

Hi

Im trying to search a text file for a certain string then grab text around it and create a new text file withy the copied text.

I attached the text file...also:
---------------------------------
Here is an example of the text file:
------------------------------------

******
TESING LOGIN FOR USER:  PLDB
ERROR:
ORA-01017: invalid username/password; logon denied


SP2-0751: Unable to connect to Oracle.  Exiting SQL*Plus
******
 
 
******
TESTING LOGIN FOR USER:  OUTLN

'CONNECTE
---------
CONNECTED

******
-------------------------------

I basically need to search for each occurrence of the word CONNECTED then I need to grab the text above it that says :   "  TESING LOGIN FOR USER:  OUTLN  "


Report.txt
LVL 8
HDatabase AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
grep -B4 CONNECTED
0
HDatabase AdministratorAuthor Commented:
that will find connected...But how would i grab text above it after it finds it then copy this text into another file then continue to the next occurrence?
0
HDatabase AdministratorAuthor Commented:
I'm very technical but a beginner with korn shell script and unix....

I ran that command like this:

INFILE=Reportzz1.txt
grep -B4 CONNECTED $INFILE


and I received these errors...??

grep: illegal option -- B
grep: illegal option -- 4
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern_list...
      [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-e pattern_list...]
      -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] pattern [file...]
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ozoCommented:
looks like you have a different version of grep
try

awk '/USER:/{u=$0}
/CONNECTED/{print u}' $INFILE
0
TintinCommented:
Unfortunately HP/UX doesn't come standard with GNU grep (the GNU versions of the common commands certainly make life *much* easier)

0
HDatabase AdministratorAuthor Commented:
hey thanks that worked...Do you mind walking me through each part?  Like how does it know to go up to the   USER: right above CONNECTED?

0
HDatabase AdministratorAuthor Commented:
also how would i just grab part of the user text?  what if I want to just store   " USER:  OUTLN "  In a variable to use later?
0
ozoCommented:
variable="USER: "`awk '/USER:/{u=$NF}
/CONNECTED/{print u}' $INFILE`
echo $variable
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HDatabase AdministratorAuthor Commented:
would you bee able to break apart these command so I can understand how they are doing what they are doing?
0
HDatabase AdministratorAuthor Commented:
Im getting this error:

CREATE_REPORT[9]: /CONNECTED/{print:  not found
USER: awk /USER:/{u=}


with the last command:

variable="USER: "'awk '/USER:/{u=$NF}
/CONNECTED/{print u}' $INFILE'
echo $variable
0
ozoCommented:
you used '
where you should have used `
0
HDatabase AdministratorAuthor Commented:
a little walk through would have been nice so i could understand what I was doing....But it did work...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.