0

I have small query and requirement regarding REGEX.

I have set of files in one location in that i want to capture only some files that are important:

The INPUT is:

```
-rw-r----- 1 osm_user osm_user 51200310 Dec  2 15:55 osm_ms01.log00025
-rw-r----- 1 osm_user osm_user 51200201 Dec  2 19:52 osm_ms01.log00026
-rw-r----- 1 osm_user osm_user 51200010 Dec  2 23:49 osm_ms01.log00027
-rw-r----- 1 osm_user osm_user  1251657 Dec  3 02:53 osm_ms01.out00098
-rw-r----- 1 osm_user osm_user    14534 Dec  3 03:23 osm_ms01.out00099
-rw-r----- 1 osm_user osm_user  2124021 Dec  9 00:42 access.log
-rw-r----- 1 osm_user osm_user  3406259 Dec  9 00:44 osm_ms01.out00100
-rw-r----- 1 osm_user osm_user    14632 Dec  9 00:55 osm_ms01.out00101
-rw-r----- 1 osm_user osm_user  4142712 Dec  9 01:00 osm_ms01-diagnostic.log
-rw-r----- 1 osm_user osm_user 51200768 Dec  9 13:20 osm_ms01.log00028
drwxr----- 2 osm_user osm_user     4096 Dec 11 00:58 metrics
-rw-r----- 1 osm_user osm_user  1101990 Dec 11 01:38 osm_ms01.out
-rw-r----- 1 osm_user osm_user  1150229 Dec 11 01:38 osm_ms01.log
```

In above input i just want to capture these below files:

```
access.log
osm_ms01-diagnostic.log
osm_ms01.log
```

I tried this below REGEX but i cant find any solution.

The regex is used

1) .*\.log.* 2) (access.log|osm_ms01.log|osm_ms01-diagnostic.log)

In second regex i can captured information but its capturing information that not necessary.

Please help me on this.

Teja R
  • 131
  • 3
  • 12

3 Answers3

1

use

(access.log|osm_ms01.log|osm_ms01-diagnostic.log)$

for matching from the ending

Andrei
  • 42,814
  • 35
  • 154
  • 218
nkit
  • 156
  • 4
  • Thanks One small doubt the one you given also working and @naurel given one regex with boundary that also matching. What diffrence between $ and /b. which one is efficient to use. ? – Teja R Dec 12 '15 at 17:58
  • /b is for matching word breaks, while $ is to match line end – nkit Dec 14 '15 at 05:31
0

I feel this is similar to this page

If you put each line into an array element, and check if the the string if it contains what you want.

Community
  • 1
  • 1
Tomaltach
  • 913
  • 1
  • 11
  • 30
0

You need to check that your .log isn't followed by another char. To do so you can check for the word boundary before and after :

\b(access.log|osm_ms01.log|osm_ms01-diagnostic.log)\b

Demo

All credit goes to @stribizhev

naurel
  • 625
  • 4
  • 18
  • 2
    Perhaps, `(?<=\s)` and `(?=\s)` can be replaced with a mere `\b`. Something like `\b(access\.log|osm_ms01\.log|osm_ms01-diagnostic\.log)\b` – Wiktor Stribiżew Dec 11 '15 at 10:56