github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/commands/rx.md (about)

     1  # `rx`
     2  
     3  > Regexp pattern matching for file system objects (eg `.*\\.txt`)
     4  
     5  ## Description
     6  
     7  Returns a list of files and directories that match a regexp pattern.
     8  
     9  Output is a JSON list.
    10  
    11  ## Usage
    12  
    13  ```
    14  rx pattern -> <stdout>
    15  
    16  !rx pattern -> <stdout>
    17  
    18  <stdin> -> rx pattern -> <stdout>
    19  
    20  <stdin> -> !rx pattern -> <stdout>
    21  ```
    22  
    23  ## Examples
    24  
    25  Inline regex file matching:
    26  
    27  ```
    28  cat: @{ rx '.*\.txt' }
    29  ```
    30  
    31  Writing a list of files to disk:
    32  
    33  ```
    34  rx '.*\.go' |> filelist.txt
    35  ```
    36  
    37  Checking if files exist:
    38  
    39  ```
    40  if { rx somefiles.* } then {
    41      # files exist
    42  }
    43  ```
    44  
    45  Checking if files do not exist:
    46  
    47  ```
    48  !if { rx somefiles.* } then {
    49      # files do not exist
    50  }
    51  ```
    52  
    53  Return all files apart from text files:
    54  
    55  ```
    56  !g '\.txt$'
    57  ```
    58  
    59  Filtering a file list based on regexp matches file:
    60  
    61  ```
    62  f +f -> rx '.*\.txt'
    63  ```
    64  
    65  Remove any regexp file matches from a file list:
    66  
    67  ```
    68  f +f -> !rx '.*\.txt'
    69  ```
    70  
    71  ## Detail
    72  
    73  ### Traversing Directories
    74  
    75  Unlike globbing (`g`) which can traverse directories (eg `g /path/*`), `rx` is
    76  only designed to match file system objects in the current working directory.
    77  
    78  `rx` uses Go (lang)'s standard regexp engine.
    79  
    80  ### Inverse Matches
    81  
    82  If you want to exclude any matches based on wildcards, rather than include
    83  them, then you can use the bang prefix. eg
    84  
    85  ```
    86  » rx READ*                                                                                                                                                              
    87  [
    88      "README.md"
    89  ]
    90  
    91  murex-dev» !rx .*
    92  Error in `!rx` (1,1): No data returned.
    93  ```
    94  
    95  ### When Used As A Method
    96  
    97  `!rx` first looks for files that match its pattern, then it reads the file list
    98  from STDIN. If STDIN contains contents that are not files then `!rx` might not
    99  handle those list items correctly. This shouldn't be an issue with `rx` in its
   100  normal mode because it is only looking for matches however when used as `!rx`
   101  any items that are not files will leak through.
   102  
   103  This is its designed feature and not a bug. If you wish to remove anything that
   104  also isn't a file then you should first pipe into either `g *`, `rx .*`, or
   105  `f +f` and then pipe that into `!rx`.
   106  
   107  The reason for this behavior is to separate this from `!regexp` and `!match`.
   108  
   109  ## Synonyms
   110  
   111  * `rx`
   112  * `!rx`
   113  
   114  
   115  ## See Also
   116  
   117  * [`f`](../commands/f.md):
   118    Lists or filters file system objects (eg files)
   119  * [`g`](../commands/g.md):
   120    Glob pattern matching for file system objects (eg `*.txt`)
   121  * [`match`](../commands/match.md):
   122    Match an exact value in an array
   123  * [`regexp`](../commands/regexp.md):
   124    Regexp tools for arrays / lists of strings
   125  
   126  <hr/>
   127  
   128  This document was generated from [builtins/core/io/rx_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/core/io/rx_doc.yaml).