Search for multiple words (patterns) in a file (multiple files) in a shell (Linux)

Configurare noua (How To)

Situatie

If you need to search for multiple words (patterns) in a file (multiple files) in a shell, you can use grep (or egrep).

Solutie

Pasi de urmat

# simple search of one (1) word (pattern):

Syntax:
egrep [options] PATTERN [FILE...]

Example :

egrep -n "ORACLE_HOME" alert_BMS.log
--- snip ---
$ egrep -n "ORACLE_HOME|ALTER DATABASE OPEN" alert_BMS.log
...
194906:ORACLE_HOME = /oracle/BMS/121
196241:ORACLE_HOME: /oracle/BMS/19.0.0
196925:ORACLE_HOME: /oracle/BMS/19.0.0
197546:ORACLE_HOME: /oracle/BMS/19.0.0
198612:ORACLE_HOME: /oracle/BMS/19.0.0
199135:ORACLE_HOME: /oracle/BMS/19.0.0
200071:ORACLE_HOME: /oracle/BMS/19.0.0
--- snip ---

It will show the number of the line inside the file and all lines that contain simple word “ORACLE_HOME” and sentience “ALTER DATABASE OPEN”.

# complex search of more then one (1) word (pattern):

Syntax:
egrep [options] "PATTERN1|PATTERN2" [FILE...]
Example : 
egrep -n "ORACLE_HOME|ALTER DATABASE OPEN" alert_BMS.log
egrep -n "ORACLE_HOME|ALTER DATABASE OPEN" *.log --- search in all files which end with ".log"
--- snip ---
$ egrep -n "ORACLE_HOME|ALTER DATABASE OPEN" alert_BMS.log
[...]
194906:ORACLE_HOME = /oracle/BMS/121
195105:ALTER DATABASE OPEN
195243:Completed: ALTER DATABASE OPEN
196241:ORACLE_HOME: /oracle/BMS/19.0.0
196478:ALTER DATABASE OPEN MIGRATE
196620:Completed: ALTER DATABASE OPEN MIGRATE
196925:ORACLE_HOME: /oracle/BMS/19.0.0
197250:ALTER DATABASE OPEN MIGRATE
197387:Completed: ALTER DATABASE OPEN MIGRATE
[...]
--- snip ---

It will show the number of the line inside the file and all lines that contain simple word “ORACLE_HOME” and sentience “ALTER DATABASE OPEN”.

Tip solutie

Permanent

Voteaza

(11 din 36 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?