Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Lines based on keyword

Status
Not open for further replies.

thiagarr

Technical User
Oct 20, 2006
86
US
Hi,

I use Open Object REXX in Windows XP machine.

I am fairly new to REXX and I need some help. I have a text file with lots of data and I need to parse certain lines based on the following logic:

The input file looks like this:

I am fairly new to REXX
and I need some help
I have a text file with lots of data
and I need to parse certain lines
based on the following logic

When I find a certain word such as 'logic', I would like to send the current line and the two previous lines concatenated into a single line and copied to a new file. For example, here is what my output file should contain:

I have a text file with lots of data and I need to parse certain lines based on the following logic

Any help would be appreciated.

Thanks and regards,

TR
 
Hi,

Thank you for the response.

That particular task has been completed withour using REXX. But if, you have time, it would be great to know how to solve this requirement using REXX for future use.

I appreciate your help.

Thank you.



Thanks and regards,

TR
 
it would be great to know how to solve this requirement using REXX for future use
OK. Here we go . . .
Code:
/*-- PedalSteel ------------------------------- 2007.05.08 16:49:43 --*/
trace 'n'

parse arg keyword .
if keyword='' then keyword='logic'

ifid='tektip.txt'
-- ofid=keyword'.txt'

ii=0; oo=0
set=3  -- number of lines in set

do while lines(ifid)  -- read file
 ii=ii+1
 iline=linein(ifid)
 found=0
 do ww=1 to words(iline)  -- look for keyword
  if word(iline,ww)<>keyword then iterate
  found=1  -- line contains keyword
  leave
 end
 oline.ii=found stream(ifid,'c','query position read')  -- store line status & location
end

oline.0=ii

call stream ifid,'c','seek =1 read'  -- rewind to start of file

do oo=1 to oline.0  -- select line sets
 oo1=oo-set
 if word(oline.oo,1) then call pout
end

exit

------------------------------------------------------------------------
pout:
 call stream ifid,'c','seek ='word(oline.oo1,2) 'read'  -- rewind to first line in set
 poline=''
 do set  -- concatenate the line set
  poline=poline linein(ifid)
 end
 say strip(poline,'L')
 -- call lineout ofid,strip(poline,'L')
return

------------------------------------------------------------------------
My tektip.txt file contains your sample text. I've commented out output file stuff, and I haven't allowed for the case where there aren't enough lines preceding the keyword line.
 
Here's a different approach.
I modified your example a little to illustrate case and special characters.

I am fairly new to REXX
and I "Need" some help
I have a text file with lots of data
and I need, to parse certain lines
based on the following logic

/* REXX Search for key word. Write 3 Lines */

InFileID = 'InputText.txt'
OutFileID = 'OutputText.txt'
KeyWord = 'NEED'

SpecialChr = '.?!"",'';:'
Line1 = ''
Line2 = ''
Line3 = ''

Do while lines(InFileID) > 0
Line1 = Line2
Line2 = Line3
Line3 = linein(InFileID)
LineX = Translate(Line3, ,SpecialChr)
LineX = Translate(LineX)
If wordpos(KeyWord,LineX) > 0 then
rc = Lineout(OutFileID,Line1 Line2 Line3)
End
rc = Lineout(OutFileID)
 
Hi Pedalsteel and RxUsr,

Thank you for your help and the sample code. I will try them and post back if there is any further requirement which I can not work out myself.

I really appreciate your help and the help of the Forum.

Thank you.

Thanks and regards,

TR
 
PedalSteel:
Instead of
Code:
 do ww=1 to words(iline)  -- look for keyword
  if word(iline,ww)<>keyword then iterate
  found=1  -- line contains keyword
  leave
 end
try
Code:
if WordPos( keyword,iline ) > 0 then ...


Frank Clarke
Tampa Area REXX Programmers' Alliance
REXX Language Assn Listmaster
 
Much more elegant, RxUsr. I must have had stream on my mind that day.

As for wordpos, Frank -- senior moment, bigtime. Come back JCL, all is forgiven.
 
Frank,

Thank you for the suggestion.

Thanks and regards,

TR
 
here's my solution (did it for personal training)
perhaps a bit ineffective cause lines are read in multiple times.
Code:
#!/usr/bin/rexx
i=1
success=0
do while lines("logic.txt")>0
        line=linein("logic.txt",i,1)
        if find(line,"logic")>0 then
        do
                success=1
                say linein("logic.txt",i-2,1)
                say linein("logic.txt",i-1,1)
                say line
                leave
        end
        i=i+1
end
if success=0 then
        say "'Logic' wasn't found in file 'logic.txt'."
 
FIND" is one of those functions that exist in REXX just so old assembler programmers will feel they haven't been left out ;-) POS and WORDPOS are preferred:
Code:
pt = FIND( haystack,needle )
pt = WORDPOS( needle,haystack )

Frank Clarke
Tampa Area REXX Programmers' Alliance
REXX Language Assn Listmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top