首页 > 解决方案 > How to use grep command to list dot based names

问题描述

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

标签: regexgrep

解决方案


您可以使用

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

这里,

  • ^- 字符串的开始
  • (sssd|autofs)- 匹配两个子字符串中的任何一个的组
  • \.service- 一个.service子串。

查看在线演示


推荐阅读