Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Regular expression to split output from ls command in bash

  • 30-01-2016 7:04pm
    #1
    Registered Users, Registered Users 2 Posts: 262 ✭✭


    Below is code. I am running an ls command and want to split the files in output. Each file contains the String "tc_" followed by a 1 to 3 digit number. Below will find files but will stop when it hits the first digit in filename.

    String output = runCommandInBash("ls -l | grep -v total | awk '{print $NF}' ")
    List<String> fileList = output .split("(?<=tc_[0-9]{3})"));


    How do i get it to first check if there are 3 digits, then 2 digits, then 1?


Comments

  • Registered Users, Registered Users 2 Posts: 6,286 ✭✭✭Talisman


    /tc_\d{1,3}/i will case incensitive match the "tc_" string followed by 1, 2, or 3 digits.


    https://regex101.com/ is a good resource for playing with regular expressions.


  • Registered Users, Registered Users 2 Posts: 897 ✭✭✭moycullen14


    guylikeme wrote: »
    Below is code. I am running an ls command and want to split the files in output. Each file contains the String "tc_" followed by a 1 to 3 digit number. Below will find files but will stop when it hits the first digit in filename.

    String output = runCommandInBash("ls -l | grep -v total | awk '{print $NF}' ")
    List<String> fileList = output .split("(?<=tc_[0-9]{3})"));


    How do i get it to first check if there are 3 digits, then 2 digits, then 1?

    What you are doing here is insane.

    First off, you don't need to use the shell, awk, ls grep or anything else to find the contents of a directory. It is hugely un-portable. java provides libraries to
    read directory contents, you should use these along with filters to perform the function you want.

    Here's a simple program that reads the contents of directories and prints out ones that match a Regular Expression. It uses the apache commons-io library (for RegexFileFilter). I would advise getting to know apache commons, it contains some great stuff and will stop you reinventing the wheel. The number of time you find exactly what you want in apache-commons is striking.
    import java.io.File;
    import java.io.FileFilter;
    import org.apache.commons.io.filefilter.RegexFileFilter;
    
    public class ReadDir {
      
      public static void main(String [] args) {
        System.out.println("Working Directory = " +
            System.getProperty("user.dir"));
        for (String arg : args) {
          File dir = new File(arg);
          FileFilter fileFilter = new RegexFileFilter("^tc_\\d{1,3}$");
          File[] files = dir.listFiles(fileFilter);
          for (int i = 0; i < files.length; i++) {
            System.out.println(arg + " --> " + files[i]);
          }
        }
      }
    
    }
    

    This will work on any system (windows, linux, etc). It will be a hell of a lot faster than your version above and doesn't depend on quirks like the last line of ls -l being 'total' . Also, using 'grep ... | awk' is a mortal sin.

    Your spec said that the files you wanted to match were tc_ followed by 1,2 or 3 digits, The regular expression {1,3} matches exactly that (it won't match, for example, tc_ followed by four digits).


Advertisement