0

what is the regular expression to capture the folder name with this structure

x.x.x.x-x ( x - integer number )

 ls /usr/hdp | .....
 2.6.0.3-8  current HG_MASTER 2.4.3 34.1 45-995

expected output

2.6.0.3-8



ls -lh /usr/hdp/
total 8.0K
drwxr-xr-x. 27 root root 4.0K Jan  1 15:24 2.6.0.3-8
7
  • As is traditional, mywiki.wooledge.org/ParsingLs is usually linked to about now. – Guy Jan 22 '18 at 9:48
  • and Don't Parse ls – cas Jan 22 '18 at 9:49
  • so what is your solution , if you not want to capture it with ls ? – jango Jan 22 '18 at 9:50
  • both of the links above offer several alternatives. e.g. find with either a -name or -regex predicate (or the case-insensitive -iname and -iregex). – cas Jan 22 '18 at 9:59
  • cas please send your solution as answer – jango Jan 22 '18 at 10:04
1

Since you tagged , you can actually do this with the shell's filename matching, using extended globs:

$ shopt -s extglob
$ touch /some/path/{2.6.0.3-8,22.66.0.333-8,foobar}
$ printf "%s\n" /some/path/+([0-9]).+([0-9]).+([0-9]).+([0-9])-+([0-9])
/some/path/22.66.0.333-8
/some/path/2.6.0.3-8

The pattern there works like more common things like * or *.png, so you can just stick the fixed parts like paths in it. Add a trailing slash / to only match directories. (the slash does appear in the output too, though.)

In ERE regex, the pattern is the same as [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+-[0-9]+. A fuzzier version would just check for the allowed characters, like +([-.0-9]) (or [-.0-9]+ in ERE), but that of course would also match e.g. 123..456.

If you want to just print the filenames, find might be easier, but if you want to run a command on them, then a shell loop is also an option (along with find -exec).

4
  • nice but how to do it when I am from other folder for example , I am in /tmp and want to capture the folder from /usr/hdp/ – jango Jan 22 '18 at 11:44
  • OP mentioned matching directories.. but that should be okay if no filenames match.. :) – Sundeep Jan 22 '18 at 11:46
  • @jango, just stick the path in the front, as with * – ilkkachu Jan 22 '18 at 11:47
  • 2
    @Sundeep, ah, true. But you can add the trailing slash to only match directories. – ilkkachu Jan 22 '18 at 11:52
1
find . -type d -regextype posix-egrep -regex '^./([0-9]+[.]){3}[0-9]+[-][0-9]+'

Use find and then the regextype flag to set the reg expression syntax to egrep. Use -regex to check against 3 digits and a "dot" and then a digit a "dash" and a digit.

If the digits are more than one character, you will need + after each digit.

6
  • why - f ( this is folder ) – jango Jan 22 '18 at 10:48
  • Yeah sorry. It's a directory and so should be d and not f for file. I will change. – Raman Sailopal Jan 22 '18 at 10:49
  • also the same your syntax not capture the folder - 2.6.4.0-91 – jango Jan 22 '18 at 10:52
  • You will need to add + to each [0-9] to signify 1 or more digits between 0 and 9. I have amended my solution – Raman Sailopal Jan 22 '18 at 11:09
  • so can you please update the answer ? – jango Jan 22 '18 at 11:32
1

You need to use find instead. For exact match use pattern:

find /usr/hdp -regextype sed -regex "\/usr\/hdp\/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*-[0-9]*"

If you need any files with only digits, . and - symbols in names try:

find /usr/hdp -regextype sed -regex "\/usr\/hdp\/[.0-9-]*"
9
  • not works on both way – jango Jan 22 '18 at 10:49
  • under /usr/hdp/ , we have 2.6.4.0-91 – jango Jan 22 '18 at 10:49
  • @jango, you did't say that filename can contain several digits consistently – Egor Vasilyev Jan 22 '18 at 10:53
  • @jango, can file contain other symbols? Can you complete your post with output of command ls -lh? – Egor Vasilyev Jan 22 '18 at 10:58
  • updated , see mu question again please – jango Jan 22 '18 at 11:22
-1
ls /usr/hdp | egrep '[0-9]+.[0-9]+.[0-9]+.[0-9]+-[0-9]+'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.