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?
 
ozoConnect With a Mentor Commented:
variable="USER: "`awk '/USER:/{u=$NF}
/CONNECTED/{print u}' $INFILE`
echo $variable
0
 
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.