Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

a keyword search with upto 3 addtional keywords close to primary keyword

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • a keyword search with upto 3 addtional keywords close to primary keyword

    a console program to find keywords, display them, then allow you to view the whole file and return back to the point in the search before viewing the whole file.

    notepad is the program used to view the whole file or it can be edited with a binary editor

    a tool to search a file or files using a wild card in a single directory using up to 4 keywords with the first keyword being a primary keyword.

    program will find and display 13 lines of a file with the line in middle being the line which holds the primary keyword.
    if additional keywords are given in the search, up to 3 of them, the results of the search will be where the additional keywords are up to 6 lines away from the primary keyword.

    option to pick where the additional keywords must be found.
    all keywords on a single line
    or
    the line on which the primary keyword is found and any line 6 lines before
    the line on which the primary keyword is found and any line 6 lines after
    the line on which the primary keyword is found and any line 6 lines before and 6 lines after.

    all keywords entered must be found for a match to be made.

    i added some code to the program while placing this thread, program should work

    discussion/addition information
    http://powerbasic.com/support/pbforu...d.php?p=279525


    Code:
    'compiled with pbcc40
    #COMPILE EXE
    '#DIM ALL
    GLOBAL FILELISTING() AS STRING
    GLOBAL LASTFILEINARRAY AS LONG
    FUNCTION PBMAIN () AS LONG
    
    FILETOSEARCH$=TRIM$(COMMAND$)
    IF FILETOSEARCH$="" THEN
        STDOUT "place the file name or wildcards names on the command line"
        GOTO FINISH2
    END IF
    
    TEMP$=DIR$(FILETOSEARCH$)
    IF TEMP$="" THEN
          STDOUT "no files found to search text in"
          GOTO FINISH2
    END IF
    DIM FILELISTING(1 TO 20000)
    LASTFILEINARRAY=1&
    FILELISTING(1&)=TEMP$
    
    FOR I&=2 TO 20000
    FILELISTING(I&)=DIR$
    IF FILELISTING(I&)="" THEN EXIT FOR
    LASTFILEINARRAY=I&
    
    NEXT I&
    IF I&=20000 THEN STDOUT "this program is limited to 20000 files
    CONSOLE SCREEN 26,120
    CONSOLE SET SCREEN 18,120
    CONSOLE SET VIRTUAL 26, 500
    START:
    DIM FILELINE$(14)
    
    COLOR 15,0
    CLS
    STDOUT "search files "+FILETOSEARCH$
    STDOUT "Matching of a primary key, can be with the secondary keys on the same line or upto 6 lines near the primary key"
    GOTO QSTRING
    START1:
    CLS
    STDOUT "the last keywords searched"
    STDOUT "primary :"+A$
    STDOUT "second  :"+B$
    STDOUT "third   :"+C$
    STDOUT "fourth  :"+D$
    
    QSTRING:
    LOCATE 6,1
    A$=""
    B$=""
    C$=""
    D$=""
    M$="Y"
    M&=0
    INPUT LINE "Additional keywords on lines .Same.Near.Before.After: ",M$
    IF LEFT$(UCASE$(TRIM$(M$)),1)="S" THEN M&=1&
    IF LEFT$(UCASE$(TRIM$(M$)),1)="N" THEN M&=2&
    IF LEFT$(UCASE$(TRIM$(M$)),1)="B" THEN M&=3&
    IF LEFT$(UCASE$(TRIM$(M$)),1)="A" THEN M&=4&
    IF LEFT$(UCASE$(TRIM$(M$)),1)="Q" THEN GOTO FINISH2
    IF LEFT$(UCASE$(TRIM$(M$)),1)="" THEN GOTO FINISH2
    
    IF M&=0 THEN GOTO QSTRING
    
    
    LOCATE 6,1
    STDOUT SPACE$(80);
    LOCATE 1,45
    IF M&=1 THEN STDOUT "secondary matching is only on the same line";
    IF M&=2 THEN STDOUT "secondary matching is same line and/or 6 lines nearby";
    IF M&=3 THEN STDOUT "secondary matching is same line and/or 6 lines before";
    IF M&=4 THEN STDOUT "secondary matching is same line and/or 6 lines after";
    
    
    LOCATE 6,1
    
    INPUT LINE "Primary string to search: ",A$
    IF TRIM$(A$)="" GOTO FINISH
    INPUT LINE "    2nd string to search: ",B$
    IF TRIM$(B$)="" GOTO STARTSEARCH
    INPUT LINE "    3rd string to search: ",C$
    IF TRIM$(C$)="" GOTO STARTSEARCH
    INPUT LINE "    4th string to search: ",D$
    IF TRIM$(D$)="" GOTO STARTSEARCH
    
    STARTSEARCH:
    
    
    ON ERROR RESUME NEXT
    FOR I&=1 TO LASTFILEINARRAY&
    MATCH&=0&
    
    ERRCLEAR
    FILEOPEN&=0&
    TEMP$=FILELISTING(I&)
    
    OPEN TEMP$ FOR INPUT AS #1  LEN=16000
    IF ERR THEN GOTO AROUND
    FILEOPEN&=1&
    AFTERENDOFFILE&=0&
    REDIM FILELINE$(14)
    LINECOUNT&=0
    ERRCLEAR
    LINE INPUT #1,FILELINE$
    
    
    WHILE ISFALSE EOF(1)
    
    ENDOFFILEVIEW:
    
    IF FILEOPEN&=1& THEN
         READANOTHERLINE:
         LINE INPUT #1,FILELINE$(14)
         INCR LINECOUNT&
        ' IF TRIM$(FILELINE$(14))="" THEN PRINT "EMPTY LINE":GOTO READANOTHERLINE
         ELSE
         INCR AFTERENDOFFILE&
         FILELINE$(14)=""
         END IF
    FOR J&=1 TO 14
        SWAP FILELINE$(J&), FILELINE$(J&-1&)
    NEXT J&
    
    
    IF INSTR(" "+UCASE$(FILELINE$(7)),UCASE$(A$))>0 THEN
       MATCH&=1&
       ELSE
       GOTO AROUND2
    END IF
    TEMPSTRING$=" "
    IF M&=1& THEN
        TEMPSTRING$=TEMPSTRING$+UCASE$(FILELINE$(7&)) 'all matching must be on same line
    END IF
    IF M&=2& THEN
        FOR J&=1 TO 13
        TEMPSTRING$=TEMPSTRING$+UCASE$(FILELINE$(J&)) 'matching can be on all 13 mulitple lines
        NEXT J&
    END IF
    
    IF M&=3& THEN
        FOR J&=1 TO 7&
        TEMPSTRING$=TEMPSTRING$+UCASE$(FILELINE$(J&)) 'matching can be on same line and 6 lines before
        NEXT J&
    END IF
    
    IF M&=4& THEN
        FOR J&=7 TO 13&
        TEMPSTRING$=TEMPSTRING$+UCASE$(FILELINE$(J&)) 'matching can be on same line and 6 lines after
        NEXT J&
    END IF
    
    IF B$<>"" THEN
       IF INSTR(TEMPSTRING$,UCASE$(B$))=0 THEN MATCH&=0&
    END IF
    
    IF C$<>"" THEN
       IF INSTR(TEMPSTRING$,UCASE$(C$))=0 THEN MATCH&=0&
    END IF
    
    IF D$<>"" THEN
       IF INSTR(TEMPSTRING$,UCASE$(D$))=0 THEN MATCH&=0&
    END IF
    
    
    
    IF MATCH&=0& THEN GOTO AROUND2
    ANSWER:
     LOCATE 11,1
    STDOUT SPACE$(499);
    LOCATE 11,1
    STDOUT TEMP$+"          match found near line"+STR$(linecount&-5&);
    
    LOCATE 26,1
    STDOUT SPACE$(499);
    
    FOR J&=1 TO 13
    LOCATE 12&+J&,1
    STDOUT SPACE$(500);
    LOCATE 12&+J&,1
    BLANK$=" "
    COLOR 15,0
    IF (J&<7 AND M&=2) OR (J&<7 AND M&=3)THEN
        IF TRIM$(B$)<>"" THEN IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(B$)) THEN COLOR 14,0
        IF TRIM$(C$)<>"" THEN IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(C$)) THEN COLOR 14,0
        IF TRIM$(D$)<>"" THEN IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(D$)) THEN COLOR 14,0
        IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(A$)) THEN COLOR 14,0
    END IF
    IF (J&>7 AND M&=2) OR (J&>7 AND M&=4) THEN
        IF TRIM$(B$)<>"" THEN IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(B$)) THEN COLOR 14,0
        IF TRIM$(C$)<>"" THEN IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(C$)) THEN COLOR 14,0
        IF TRIM$(D$)<>"" THEN IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(D$)) THEN COLOR 14,0
        IF INSTR(" "+UCASE$(FILELINE$(J&)),UCASE$(A$)) THEN COLOR 14,0
    END IF
    
    IF J&=7 THEN COLOR 10,0
    
    STDOUT FILELINE$(J&);
    COLOR 15,0
    NEXT J&
    CAVQUESTION:
    LOCATE 10,1
    STDOUT "                                                               "
    LOCATE 10,1
    INPUT LINE "Continue,Quit,View : ",YN$
    YN$=UCASE$(TRIM$(YN$))
    IF AFTERENDOFFILE&>0 THEN
        IF YN$="Q" THEN GOTO START1
    END IF
    
    IF YN$="Q" THEN CLOSE 1:GOTO START1
    IF YN$="C" THEN GOTO AROUND2
    IF YN$="" THEN GOTO AROUND2
    viewertouse$="NOTEPAD.EXE                                                                                                                            "
    viewertouse$=trim$(viewertouse$)
    IF AFTERENDOFFILE&>0 THEN
        IF YN$="V" THEN SHELL viewertouse$+" "+TEMP$
    END IF
    IF AFTERENDOFFILE&=0 THEN
    IF YN$="V" THEN CLOSE 1:SHELL viewertouse$+" "+TEMP$
    END IF
    IF YN$="V" THEN GOTO CAVQUESTION
    
    GOTO ANSWER
    
    AROUND2:
    IF AFTERENDOFFILE&>6 THEN GOTO AROUND:
    IF FILEOPEN&=0 THEN GOTO ENDOFFILEVIEW
    
    WEND
    CLOSE 1
    FILEOPEN&=0&
    GOTO ENDOFFILEVIEW
    
    
    AROUND:
    NEXT I&
    GOTO START
    
    FINISH:
    CLS
    FINISH2:
    CONSOLE SCREEN 25, 80
    END FUNCTION
    Last edited by Paul Purvis; 19 Mar 2008, 03:24 PM.
    p purvis
Working...
X