1

egrep regular expression + print specific names from files with egrep

I want to print only the following Linux machines names from /tmp/file.txt

while xx is number as 01 - 999

Linux_serverXX
Linux_agentXX
Linux_masterXX

The file is

more /tmp/file.txt

Linux_server01
Linux_server02
Linux_server03
Linux_main01
Linux_main02
Linux_agent01
Linux_agent02
Linux_agent03
Linux_domain_server01
Linux_master01
Linux_master02
Linux_master02
Linux_alert01
Linux_alert02

So we did that

egrep "Linux_server[[:digit:]]|Linux_agent[[:digit:]]|Linux_master[[:digit:]]" /tmp/file

is it good enough approach to exclude the names that we want?

Submitted November 29th 2020 by Admin

Answers
0

Could you please try following, written and tested with shown samples in GNU awk.

awk 'match($0,/Linux_(server|agent|master)[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file

OR use following in case you have multiple matches on a single line this could help then:

awk '
{ while(match($0,/Linux_(server|agent|master)[0-9]+/)){ print substr($0,RSTART,RLENGTH) $0=substr($0,RSTART+RLENGTH) }
}
' Input_file

Admin | 10 months ago



Relevant Questions