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

Java Regular expression to split output from ls command in bash

Options
  • 30-01-2016 8:04pm
    #1
    Registered Users 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 Posts: 6,037 ✭✭✭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 Posts: 870 ✭✭✭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