

* You have to specify pipes (|) separating the values for the regular expression search */ Options symbolgen /* For debugging purposes */ Set sashelp.cars (where = (make ^= "Toyota")) * If you don't find the pattern in the macro call below (prxmatch = 0), then load sashelp.cars where make isn't Toyota */ Set sashelp.cars (where = (make = "Toyota")) %if %sysfunc(prxmatch("&values.", &_aud_infile.)) > 0 %then %do /* notice that I have the values parameter in quotes. * If you find the pattern specified in the macro call below (prxmatch > 0), then load sashelp.cars where the make is Toyota */ You'd need to provide another parameter/modify the macro if you have multiple. solution is more concise and what I'd suggest given that mine may not be totally intuitive, but I think it's always helpful to see multiple solutions.Īlso, my solution assumes there's only one macro variable you're trying to parse. 3.3.Here's another solution using regular expressions. Next, let’s get a file list excluding “ warning“s: $ ls -lĪs the output above shows, the command has produced the expected result, although one filename with “ warning” contains a newline character. The GNU ls has provided the -I option to ignore glob patterns. Most modern Linux distributions use the GNU ls from the pre-installed GNU coreutils package. Therefore, the ls | grep -v approach may work for some cases. It cannot handle the filenames with newlines correctly. This is because grep is a line-based pattern-matching tool. The filename ‘some’$’\n”whatever warning.txt’ has been broken. Obviously, the output above isn’t expected. Now, let’s use the ls | grep approach to list all files whose names have “ warning“: $ ls -l | grep 'warning' The ls -l output displays the filename as ‘some’$’\n”whatever warning.txt’. rw-r-r- 1 kent kent 0 Mar 21 23:36 'some'$'\n''whatever warning.txt'Īs the output above shows, we’ve created a file whose name contains newline and space characters. So next, let’s create a new file under this directory: $ touch "some The grep command simply checks each output line and cannot tell whether a match is in the filename part.Īpart from that, many filesystems under Linux support special characters in filenames, such as space and newlines. This is because all files’ owners and groups are “ kent” too. This time, the command list all files under the directory. rw-r-r- 1 kent kent 0 Mar 21 21:24 warning.txt rw-r-r- 1 kent kent 0 Mar 21 21:24 system_warning.log Now, let’s see what ls | grep produces: $ ls -l | grep 'kent' Under the current directory, as we can see, only the kent.file should be in the output. Let’s say we want to get a list of files whose name contains “ kent“. However, “grepping” on ls output is considered a bad practice, and we shouldn’t go for it.Ī couple of examples can quickly explain the reason. rw-r-r- 1 kent kent 0 Mar 21 21:24 user.logĪs we can see, all filenames containing “ warning” has been filtered out. rw-r-r- 1 kent kent 0 Mar 21 21:24 server.log


rw-r-r- 1 kent kent 0 Mar 21 21:37 readme.txt l (lower-case L) can be added to just give the file name of matching files. rw-r-r- 1 kent kent 0 Mar 21 21:59 kent.file r or -R is recursive, -n is line number and -w stands match the whole word. rw-r-r- 1 kent kent 0 Mar 21 21:24 console.log Method 1: grep command grep command in Linux that is used to search for files containing a specific text or string. The results are then piped into another xargs command for string3 - its the same as the first xargs call, but looking for a different string. Simply finding a substring in the name is not possible without using wildcards to match the rest of the name. The results are piped into xargs which runs one or more grep commands on those files for string2. Unlike grep however, the find command is a little more strictyou need to use single or double quotes to escape the search string, and you need to use wildcards to match the entire string. So, why not pipe the output of ls -l to grep -v? First, let’s see if it works with our example: $ ls -l | grep -v 'warning' The first grep recursively finds the names of files containing string1 within path-to-files.
