Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Is it possible to search a file for a particular string and then print that string out with x amount of trailing characters?

Is it possible to search a file for a particular string and then print that string out with x amount of trailing characters?

This can be done by using MatchStr function to find the string within the document, then using the strPos function to determine the string"s position in the document. You can then use the SubStr function to return the match and x amount of characters following it.

e.g.

[-] findMatch (string sFile, STRING sMatchItem, INTEGER iNumOfChars)
    [ ] HFILE OutputFileHandle
    [ ] FILESHARE fShare
    [ ] fShare = FS_DENY_NONE 
    [ ] OutputFileHandle = FileOpen(sFile,FM_READ,fShare)
    [ ]  
    [ ] STRING sLine
    [ ] int Pos
    [ ] Int NumOfMatches = 0
    [ ]  
    [-] while (FileReadLine (OutputFileHandle, sLine))
        [-] if (MatchStr("*"+sMatchItem+"*",sLine))
            [ ]  
            [ ] //get the position of the matched string within the line.
            [ ] Pos = StrPos(sMatchItem, sLine, TRUE)
            [ ]  
            [ ] //Print x characters from where the string begins.
            [ ] Print(SubStr (sLine, Pos, iNumOfChars))
            [ ]  
            [ ] //increment the number of matches
            [ ] ++NumOfMatches
            [ ]   
    [ ] // Print the number of times a match was found
    [ ] Print ("The Number of Matches: {NumOfMatches}")

    
 
[-] main ()
    [ ] // Find how many times the string appears in the document and print out each time
    [ ] // 10 chars from where the string begins. 
    [ ] 
    [ ] // Here is some sample data .....
    [ ] string sFile = "C:\Data.txt"
    [ ] STRING sMatchItem = "Mary"
    [ ] INTEGER iNumOfChars = 10        
    [ ] 
    [ ] findMatch(sFile, sMatchItem, iNumOfChars)

sFile is the text file that you want to be searched
sMatchItem is the string that you want to search for
iNumOfChars is the amount of characters you want to be returned

Old KB# 21326

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:19
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.