user8458838
user8458838

Reputation:

regex in ubuntu to exclude a file named 0 specifically

I have the following file names:

1
2
3
4
0
0.1
0.2
1e-0.3
1e-3
a
v

I am using Ubuntu, and would like to grep all numeric values except the value 0.

I tried ls | grep -E '[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?' and it gets all numerical entries with the exponential.

But I would like to exclude the file named 0. Only the 0, and not 0.1 or 0.2

The desired output is:

1
2
3
4
0.1
0.2
1e-0.3
1e-3

Where all numeric files are listed except the file 0.

Best Regards

Upvotes: 2

Views: 126

Answers (3)

Dominique
Dominique

Reputation: 17565

I believe you need two things:

In order to skip, you need "grep -v".

The filename 0 contains three characters:

  • beginning of line
  • the 0 character
  • end of line

I think that beginning of line is "$", and end of line is "%", so you do something like:

grep -v "$0%"

You also need to do "ls" getting every filename on another line.

Upvotes: 0

anubhava
anubhava

Reputation: 786081

You may use awk without using any regex:

find . -type f -maxdepth 1 | awk -F/ '+$2'

./1
./2
./3
./4
./0.1
./0.2
./1e-0.3
./1e-3

Here, -F/ sets delimiter as / and $2 gets us the filename. Then condition +$2 returns true if $2 is numeric and > 0.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133760

With your shown samples, could you please try following. Try with find command.

find . -regextype egrep -regex '.*/([-+]?((([1-8]+{1,}|[0-9]{2,})(\.[0-9]+)?)|(0+\.[0-9]+))|([0-9]+[eE][-+]?([0-9]+?(\.[0-9]+)?))?)$'

Let's say we have following files(test purposes):

1
4
3
2
0.1
0
1e-3
1e-0.3
0.2

After running code we will get following files.

./4
./2
./1e-3
./1e-0.3
./0.2
./3
./1
./0.1

Explanation: Adding detailed explanation for above.

.*/    ##Matching till last slash here.
(
  [-+]?((([1-8]+{1,}|[0-9]{2,})(\.[0-9]+)?)|(0+\.[0-9]+))
       ##Matching file names which starts from +or- and not have only 0 in their name(could have float numbers also optional).
  |    ##putting OR condition
  ([0-9]+[eE][-+]?([0-9]+?(\.[0-9]+)?))?
       ##Matching file names which have 1e-03 OR 1e+3 like names here.
)$

Upvotes: 0

Related Questions