Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1902
  • Last Modified:

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
0
H
Asked:
H
  • 7
  • 4
1 Solution
 
ozoCommented:
grep -B4 CONNECTED
0
 
HAuthor 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
 
HAuthor 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
HAuthor 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
 
HAuthor 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
 
HAuthor Commented:
would you bee able to break apart these command so I can understand how they are doing what they are doing?
0
 
HAuthor 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
 
HAuthor Commented:
a little walk through would have been nice so i could understand what I was doing....But it did work...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now