Warm tip: This article is reproduced from serverfault.com, please click

How to use grep command to list dot based names

发布于 2020-12-02 15:06:24

I am trying to grep some of the services but it does not print exactly what I am looking for.

One thing I see in the grep strings is that the names are having dot . jointed names.

I am trying below but not getting the desired output.

# systemctl list-unit-files | egrep -w "autofs.service|sssd.service"
autofs.service                                disabled
sssd-autofs.service                           indirect
sssd.service                                  disabled

or

# systemctl list-unit-files | egrep "autofs|sssd[.service]"
autofs.service                                disabled
sssd-autofs.service                           indirect
sssd.service                                  disabled
sssd-autofs.socket                            disabled

Expected Output:

# systemctl list-unit-files | egrep -w "autofs.service|sssd.service"
autofs.service                                disabled
sssd.service                                  disabled
Questioner
kulfi
Viewed
0
Wiktor Stribiżew 2020-12-02 23:10:44

You can use

grep -E '^(sssd|autofs)\.service'

Here,

  • ^ - start of string
  • (sssd|autofs) - a group matching either of the two substrings
  • \.service - a .service substring.

See an online demo.