Ubuntu终端中文件内容的关键词匹配

Ubuntu终端中文本内容的关键词匹配

对于一个内容很多的文件,如果需要查找某个关键词及其所在的位置,可以使用grep命令进行搜索。grep命令是一个非常强大的文本处理命令,主要功能是根据关键词对文本进行筛选,查找匹配的关键词并输出位置,grep命令提供了许多选项,常用选项如下所示。

  • -a:不要忽略二进制数据
  • -b:出现时查找到的行号外,还匹配显示字符所在的整个文档的位置
  • -c:显示匹配关键词的内容和行数合计
  • -e:指定关键词,使用该选项可指定多个关键词
  • -E:指定正则表达式
  • -i:查找是不区分大小写
  • -n:显示匹配行的行号
  • -w:显示和关键词完全匹配的内容
  • -o:只输出文件中匹配到的内容

接下来以.bashrc文件为例进行演示grep命令的使用方法

#使用grep命令搜索bashrc文件中含有if的行
os@DESKTOP-2DHQBVS:~$ grep if .bashrc
# check the window size after each command and, if necessary,
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
# uncomment for a colored prompt, if the terminal has the capability; turned
if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
if [ "$color_prompt" = yes ]; then
if [ -x /usr/bin/dircolors ]; then
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
if [ -f ~/.bash_aliases ]; then
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
  elif [ -f /etc/bash_completion ]; then

#使用-b选项,能够输出匹配关键词的行号
os@DESKTOP-2DHQBVS:~$ grep -b if .bashrc
544:# check the window size after each command and, if necessary,
957:# set variable identifying the chroot you work in (used in the prompt below)
1034:if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
1282:# uncomment for a colored prompt, if the terminal has the capability; turned
1515:if [ -n "$force_color_prompt" ]; then
1553:    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
1855:if [ "$color_prompt" = yes ]; then
2334:if [ -x /usr/bin/dircolors ]; then
2958:alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
3334:if [ -f ~/.bash_aliases ]; then
3460:# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
3558:if ! shopt -oq posix; then
3585:  if [ -f /usr/share/bash-completion/bash_completion ]; then
3695:  elif [ -f /etc/bash_completion ]; then

#使用-w选项进行全字匹配,即必须与制定关键词完全匹配才可以搜索到
#即使包含关键词也不可以被匹配到
os@DESKTOP-2DHQBVS:~$ grep -bw if .bashrc
544:# check the window size after each command and, if necessary,
1034:if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
1282:# uncomment for a colored prompt, if the terminal has the capability; turned
1515:if [ -n "$force_color_prompt" ]; then
1553:    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
1855:if [ "$color_prompt" = yes ]; then
2334:if [ -x /usr/bin/dircolors ]; then
3334:if [ -f ~/.bash_aliases ]; then
3460:# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
3558:if ! shopt -oq posix; then
3585:  if [ -f /usr/share/bash-completion/bash_completion ]; then

#使用-o选项能够输出匹配项的内容,而不是所在行的所有内容
os@DESKTOP-2DHQBVS:~$ grep -bwo if .bashrc
592:if
1034:if
1316:if
1515:if
1557:if
1855:if
2334:if
3334:if
3468:if
3558:if
3587:if

你可能感兴趣的:(Ubuntu终端中文件内容的关键词匹配)