Inter-active setting of /grep options for TPIPE

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
Further to https://jpsoft.com/forums/threads/tpipe-aliases.8737/#post-49148

TGREP.BTM will allow the inter-active setting of /grep options for TPIPE.

After setting arguments, use TGREP.BTM as a filter.

Examples;
Code:
tgrep.btm
1514808436569.png

1514808472706.png

Code:
c:\users\jlc\utils>tgrep
Type:               3
IncludeLineNumbers: 0
IncludeFileName:    0
MatchCase:          0
CountMatches:       0
PatternType:        0
UTF8:               0
IgnoreEmpty:        0
tpipe /grep=3,0,0,0,0,0,0,0,"IBM Corp"
Now, use TGREP.BTM as a filter;
Code:
c:\users\jlc\utils>type macadr.txt | tgrep IBM Corp
00:02:55        Ibm                    # IBM Corp
00:09:6B        Ibm                    # IBM Corp
00:0D:60        Ibm                    # IBM Corp
00:11:25        Ibm                    # IBM Corp
00:14:5E        Ibm                    # IBM Corp
00:17:EF        Ibm                    # IBM Corp
00:18:B1        Ibm                    # IBM Corp
00:1A:64        Ibm                    # IBM Corp
00:21:5E        Ibm                    # IBM Corp
00:22:00        Ibm                    # IBM Corp
00:25:03        Ibm                    # IBM Corp
00:50:76        Ibm                    # IBM Corp
08:17:F4        Ibm                    # IBM Corp
5C:F3:FC        Ibm                    # IBM Corp
6C:AE:8B        Ibm                    # IBM Corporation
74:99:75        Ibm                    # IBM Corporation
E4:1F:13        Ibm                    # IBM Corp
FC:CF:62        Ibm                    # IBM Corp
If I wanted the line numbers displayed, I execute tgrep.btm, make the changes to IncludeLineNumbers;
1514808546583.png
1514808558345.png
Code:
c:\users\jlc\utils>tgrep
Type:               3
IncludeLineNumbers: 1
IncludeFileName:    0
MatchCase:          0
CountMatches:       0
PatternType:        0
UTF8:               0
IgnoreEmpty:        0
tpipe /grep=3,1,0,0,0,0,0,0,"IBM Corp"
...then re-run my filter;
Code:
c:\users\jlc\utils>type macadr.txt | tgrep IBM Corp
654 00:02:55    Ibm                    # IBM Corp
2441 00:09:6B   Ibm                    # IBM Corp
3453 00:0D:60   Ibm                    # IBM Corp
4418 00:11:25   Ibm                    # IBM Corp
5243 00:14:5E   Ibm                    # IBM Corp
6156 00:17:EF   Ibm                    # IBM Corp
6350 00:18:B1   Ibm                    # IBM Corp
6785 00:1A:64   Ibm                    # IBM Corp
8772 00:21:5E   Ibm                    # IBM Corp
8934 00:22:00   Ibm                    # IBM Corp
9702 00:25:03   Ibm                    # IBM Corp
10910 00:50:76  Ibm                    # IBM Corp
17494 08:17:F4  Ibm                    # IBM Corp
21896 5C:F3:FC  Ibm                    # IBM Corp
22522 6C:AE:8B  Ibm                    # IBM Corporation
24465 74:99:75  Ibm                    # IBM Corporation
29510 E4:1F:13  Ibm                    # IBM Corp
30635 FC:CF:62  Ibm                    # IBM Corp
Here is the source to TGREP.BTM;
Code:
:: TGREP.BTM
:: Makes using the /grep option of TPIPE more manageable.
::
:: USAGE: TYPE TEXT.TXT | TGREP.BTM Pattern
::
:: To change the /grep arguments;
:: USAGE: TGREP.BTM
::
:: NOTES:
:: This is a self-modifying .BTM
:: Does not work after being BATCOMPed, as a compiled .BTM
::  cannot be modified.

@setlocal
@echo off

::Will not work in a directory with white space
iff %@count[ ,%_cwd] gt 0 then
  echo %0 will not work in a directory with white space
  Goto EndJob
endiff

:: Make sure that this was started with the .BTM extension
iff %@ext[%0] ne btm then
  call "%0.btm" %$
  Goto EndJob
endiff

::Create a temporary scratch file;
set TempFile="%@unique[]"

:: If STDIN is console...
iff %_stdin eq 1 then
  Gosub GrepArgs
  Gosub theGrep
  :: /grep= is in the %TempFile
  echo %theGrep
  Goto EndJob
endiff

iff %# gt 0 then
  iff %_BatchType eq 0 then
    :: This will modify this .BTM as it is executing
    echo %@iniwrite[.\%0,config,Pattern,%$] > nul
  else
    echo %0 will not work after being BATCOMPed
    Goto EndJob
  endiff
else
  echo USAGE: %0 Pattern
  Goto EndJob
endiff

:: Execute TPIPE with the /grep arguments
tpipe /grep=^
%@iniread[.\%0,config,Type],^
%@iniread[.\%0,config,IncludeLineNumbers],^
%@iniread[.\%0,config,IncludeFileName],^
%@iniread[.\%0,config,MatchCase],^
%@iniread[.\%0,config,CountMatches],^
%@iniread[.\%0,config,PatternType],^
%@iniread[.\%0,config,UTF8],^
%@iniread[.\%0,config,IgnoreEmpty],^
"%@iniread[.\%0,config,Pattern]"
Goto EndJob
endlocal

:CountMatches
  text > %TempFile
0 to output a count of the number of matches
1 to output a count of the number of matches
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,CountMatches]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,CountMatches,%@word[0,%ArgValue]] > nul
  endiff
Return

:DisplayArgs
  Gosub GetArgs
  :: Argument list is in the %TempFile
  Type %TempFile
Return

:EndJob
  :: Delete the temporary scratch file
  if exist %TempFile del /q %TempFile
  quit
Return

:GetArgs
  type <<- endtext > %TempFile
Type:               %@iniread[.\%0,config,Type]
IncludeLineNumbers: %@iniread[.\%0,config,IncludeLineNumbers]
IncludeFileName:    %@iniread[.\%0,config,IncludeFileName]
MatchCase:          %@iniread[.\%0,config,MatchCase]
CountMatches:       %@iniread[.\%0,config,CountMatches]
PatternType:        %@iniread[.\%0,config,PatternType]
UTF8:               %@iniread[.\%0,config,UTF8]
IgnoreEmpty:        %@iniread[.\%0,config,IgnoreEmpty]
  endtext
Return

:GrepArgs
  Gosub GetArgs
  set theArg=%@select[%TempFile,1,1,25,80,Select an argument to modify]
  iff %SELECT_LINE gt 0 then
    Gosub GrepArg
    Gosub DisplayArgs
  endiff
Return

:GrepArg
  set ArgName=%@word[":",0,%theArg]
  set ArgValue=%@word[1,%theArg]
  switch %ArgName
  case Type
    Gosub Type
  case IncludeLineNumbers
    Gosub IncludeLineNumbers
  case IncludeFileName
    Gosub IncludeFileName
  case MatchCase
    Gosub MatchCase
  case CountMatches
    Gosub CountMatches
  case PatternType
    Gosub PatternType
  case UTF8
    Gosub UTF8
  case IgnoreEmpty
    Gosub IgnoreEmpty
  endswitch
Return

:IgnoreEmpty
  text > %TempFile
0 to not ignore empty matches
1 to ignore empty matches
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,IgnoreEmpty]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,IgnoreEmpty,%@word[0,%ArgValue]] > nul
  endiff
Return

:IncludeLineNumbers
  text > %TempFile
0 to not include the line number where the pattern was found
1 to include the line number where the pattern was found
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Include Line Numbers]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,IncludeLineNumbers,%@word[0,%ArgValue]] > nul
  endiff
Return

:IncludeFileName
  text > %TempFile
0 to not include the filename where the pattern was found
1 to include the filename where the pattern was found
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Include Filename]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,IncludeFilename,%@word[0,%ArgValue]] > nul
  endiff
Return

:MatchCase
  text > %TempFile
0 to not do a case-sensitive comparison when matching the pattern
1 to do a case-sensitive comparison when matching the pattern
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,MatchCase]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,MatchCase,%@word[0,%ArgValue]] > nul
  endiff
Return

:PatternType
  text > %TempFile
0 Perl pattern
1 Egrep pattern
2 Brief pattern
3 MS Word pattern
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Select the PatternType]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,PatternType,%@word[0,%ArgValue]] > nul
  endiff
Return

:theGrep
set theGrep=tpipe /grep=^
%@iniread[.\%0,config,Type],^
%@iniread[.\%0,config,IncludeLineNumbers],^
%@iniread[.\%0,config,IncludeFileName],^
%@iniread[.\%0,config,MatchCase],^
%@iniread[.\%0,config,CountMatches],^
%@iniread[.\%0,config,PatternType],^
%@iniread[.\%0,config,UTF8],^
%@iniread[.\%0,config,IgnoreEmpty],^
"%@iniread[.\%0,config,Pattern]"
Return

:Type
  text > %TempFile
0 Restrict lines matching (for subfilters)
1 Restrict lines NOT matching (for subfilters)
2 Extract matches
3 Extract matching lines (grep)
4 Extract non-matching lines (inverse grep)
5 Remove matching lines
6 Remove non-matching lines
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Select the Type]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,Type,%@word[0,%ArgValue]] > nul
  endiff
Return

:UTF8
  text > %TempFile
0 to not allow matching Unicode UTF8 characters
1 to allow matching Unicode UTF8 characters
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,UTF8]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\%0,config,UTF8,%@word[0,%ArgValue]] > nul
  endiff
Return

[config]
; Type:
;0 Restrict lines matching (for subfilters)
;1 Restrict lines NOT matching (for subfilters)
;2 Extract matches
;3 Extract matching lines (grep)
;4 Extract non-matching lines (inverse grep)
;5 Remove matching lines
;6 Remove non-matching lines
Type =3

; IncludeLineNumbers - 1 to include the line number where the pattern was found
IncludeLineNumbers =0

; IncludeFilename - 1 to include the filename where the pattern was found
IncludeFileName =0

; MatchCase - 1 to do a case-sensitive comparison when matching the pattern
MatchCase =0

; CountMatches - 1 to output a count of the number of matches
CountMatches =0

; PatternType
; 0 Perl pattern
; 1 Egrep pattern
; 2 Brief pattern
; 3 MS Word pattern
PatternType =0

; UTF8 - 1 to allow matching Unicode UTF8 characters
UTF8 =0

; IgnoreEmpty - 1 to ignore empty matches
IgnoreEmpty =0

; Pattern - the (regular expression) pattern to match
Pattern =IBM Corp
Joe
 

Attachments

#2
A few fixes...
Code:
:: TGREP.BTM
:: Makes using the /grep option of TPIPE more manageable.
::
:: USAGE: TYPE TEXT.TXT | TGREP.BTM Pattern
::
:: To change the /grep arguments;
:: USAGE: TGREP.BTM
::
:: NOTES:
:: This is a self-modifying .BTM
:: Does not work after being BATCOMPed, as a compiled .BTM
::  cannot be modified.
::
:: 2018/01/02
:: Now works in a directory with white space
:: Copies tgrep.btm to _cwd, executes tgrep.btm , deletes tgrep.btm
:: Quoted %0 for path/filename with whitespace.

@setlocal
@echo off

:: Make sure that this was started with the .BTM extension
iff %@ext["%0"] ne btm then
  which "%0" > nul
  call "%0.btm" %$
  Goto EndJob
endiff

:: 2018/01/02
iff %_? ne 0 then
  echo Could not locate "%0"
  Goto EndJob
else
  iff not exist "%0" then
    set tgrep=%@word[-0,%@execstr[which tgrep.btm]]
    copy /q %tgrep
    call "%0" %$
    if exist "%0" copy /q "%0" %tgrep
    if exist "%0" del /q "%0"
    Goto EndJob
  endiff
endiff

::Create a temporary scratch file;
set TempFile="%@unique[]"

:: If STDIN is console...
iff %_stdin eq 1 then
  Gosub GrepArgs
  Gosub theGrep
  :: /grep= is in the %TempFile
  echo %theGrep
  Goto EndJob
endiff

iff %# gt 0 then
  iff %_BatchType eq 0 then
    :: This will modify this .BTM as it is executing
    echo %@iniwrite[.\"%0",config,Pattern,%$] > nul
  else
    echo "%0" will not work after being BATCOMPed
    Goto EndJob
  endiff
else
  echo USAGE: "%0" Pattern
  Goto EndJob
endiff

:: Execute TPIPE with the /grep arguments
tpipe /grep=^
%@iniread[.\"%0",config,Type],^
%@iniread[.\"%0",config,IncludeLineNumbers],^
%@iniread[.\"%0",config,IncludeFileName],^
%@iniread[.\"%0",config,MatchCase],^
%@iniread[.\"%0",config,CountMatches],^
%@iniread[.\"%0",config,PatternType],^
%@iniread[.\"%0",config,UTF8],^
%@iniread[.\"%0",config,IgnoreEmpty],^
"%@iniread[.\"%0",config,Pattern]"
Goto EndJob
endlocal

:CountMatches
  text > %TempFile
0 to output a count of the number of matches
1 to output a count of the number of matches
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,CountMatches]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,CountMatches,%@word[0,%ArgValue]] > nul
  endiff
Return

:DisplayArgs
  Gosub GetArgs
  :: Argument list is in the %TempFile
  Type %TempFile
Return

:EndJob
  :: Delete the temporary scratch file
  if exist %TempFile del /q %TempFile
  quit
Return

:GetArgs
  type <<- endtext > %TempFile
Type:               %@iniread[.\"%0",config,Type]
IncludeLineNumbers: %@iniread[.\"%0",config,IncludeLineNumbers]
IncludeFileName:    %@iniread[.\"%0",config,IncludeFileName]
MatchCase:          %@iniread[.\"%0",config,MatchCase]
CountMatches:       %@iniread[.\"%0",config,CountMatches]
PatternType:        %@iniread[.\"%0",config,PatternType]
UTF8:               %@iniread[.\"%0",config,UTF8]
IgnoreEmpty:        %@iniread[.\"%0",config,IgnoreEmpty]
  endtext
Return

:GrepArgs
  Gosub GetArgs
  set theArg=%@select[%TempFile,1,1,25,80,Select an argument to modify]
  iff %SELECT_LINE gt 0 then
    Gosub GrepArg
    Gosub DisplayArgs
  endiff
Return

:GrepArg
  set ArgName=%@word[":",0,%theArg]
  set ArgValue=%@word[1,%theArg]
  switch %ArgName
  case Type
    Gosub Type
  case IncludeLineNumbers
    Gosub IncludeLineNumbers
  case IncludeFileName
    Gosub IncludeFileName
  case MatchCase
    Gosub MatchCase
  case CountMatches
    Gosub CountMatches
  case PatternType
    Gosub PatternType
  case UTF8
    Gosub UTF8
  case IgnoreEmpty
    Gosub IgnoreEmpty
  endswitch
Return

:IgnoreEmpty
  text > %TempFile
0 to not ignore empty matches
1 to ignore empty matches
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,IgnoreEmpty]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,IgnoreEmpty,%@word[0,%ArgValue]] > nul
  endiff
Return

:IncludeLineNumbers
  text > %TempFile
0 to not include the line number where the pattern was found
1 to include the line number where the pattern was found
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Include Line Numbers]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,IncludeLineNumbers,%@word[0,%ArgValue]] > nul
  endiff
Return

:IncludeFileName
  text > %TempFile
0 to not include the filename where the pattern was found
1 to include the filename where the pattern was found
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Include Filename]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,IncludeFilename,%@word[0,%ArgValue]] > nul
  endiff
Return

:MatchCase
  text > %TempFile
0 to not do a case-sensitive comparison when matching the pattern
1 to do a case-sensitive comparison when matching the pattern
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,MatchCase]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,MatchCase,%@word[0,%ArgValue]] > nul
  endiff
Return

:PatternType
  text > %TempFile
0 Perl pattern
1 Egrep pattern
2 Brief pattern
3 MS Word pattern
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Select the PatternType]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,PatternType,%@word[0,%ArgValue]] > nul
  endiff
Return

:theGrep
set theGrep=tpipe /grep=^
%@iniread[.\"%0",config,Type],^
%@iniread[.\"%0",config,IncludeLineNumbers],^
%@iniread[.\"%0",config,IncludeFileName],^
%@iniread[.\"%0",config,MatchCase],^
%@iniread[.\"%0",config,CountMatches],^
%@iniread[.\"%0",config,PatternType],^
%@iniread[.\"%0",config,UTF8],^
%@iniread[.\"%0",config,IgnoreEmpty],^
"%@iniread[.\"%0",config,Pattern]"
Return

:Type
  text > %TempFile
0 Restrict lines matching (for subfilters)
1 Restrict lines NOT matching (for subfilters)
2 Extract matches
3 Extract matching lines (grep)
4 Extract non-matching lines (inverse grep)
5 Remove matching lines
6 Remove non-matching lines
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,Select the Type]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,Type,%@word[0,%ArgValue]] > nul
  endiff
Return

:UTF8
  text > %TempFile
0 to not allow matching Unicode UTF8 characters
1 to allow matching Unicode UTF8 characters
  endtext
  set ArgValue=%@select[%TempFile,1,1,25,80,UTF8]
  iff %SELECT_LINE gt 0 then
    echo %@iniwrite[.\"%0",config,UTF8,%@word[0,%ArgValue]] > nul
  endiff
Return

[config]
; Type:
;0 Restrict lines matching (for subfilters)
;1 Restrict lines NOT matching (for subfilters)
;2 Extract matches
;3 Extract matching lines (grep)
;4 Extract non-matching lines (inverse grep)
;5 Remove matching lines
;6 Remove non-matching lines
Type =3

; IncludeLineNumbers - 1 to include the line number where the pattern was found
IncludeLineNumbers =0

; IncludeFilename - 1 to include the filename where the pattern was found
IncludeFileName =0

; MatchCase - 1 to do a case-sensitive comparison when matching the pattern
MatchCase =0

; CountMatches - 1 to output a count of the number of matches
CountMatches =0

; PatternType
; 0 Perl pattern
; 1 Egrep pattern
; 2 Brief pattern
; 3 MS Word pattern
PatternType =0

; UTF8 - 1 to allow matching Unicode UTF8 characters
UTF8 =0

; IgnoreEmpty - 1 to ignore empty matches
IgnoreEmpty =0

; Pattern - the (regular expression) pattern to match
Pattern =IBM