美文网首页
Ubuntu终端中文件内容的关键词匹配

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

作者: vvaa01 | 来源:发表于2018-10-02 22:42 被阅读0次

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终端中文件内容的关键词匹配

    Ubuntu终端中文本内容的关键词匹配 对于一个内容很多的文件,如果需要查找某个关键词及其所在的位置,可以使用gr...

  • Ubuntu终端中的文件内容显示

    文件内容显示 前面已经说过了文件的创建、操作等命令及方法,除去这些以外,最重要的其实还是文件的查看,如果每个文件的...

  • Ubuntu终端中的文件搜索

    搜索文件 和新建文件类似的,在终端中提供了多种方式进行文件的搜索,常用的也是三种方式。 which命令对于whic...

  • Ubuntu终端中的文件操作

    文件操作 在Windows等平台中,经常会用到的文件操作有复制、剪切、删除等,在桌面环境中进行这些操作,通常需要使...

  • 使用qtcreator进行OpenFOAM项目构建

    1、安装qtcreator 在ubuntu终端输入: 2、新建项目 打开qtcreator; 点击文件->新建文件...

  • 账户关键词匹配模式

    关键词匹配度是指关键词和页面内容中所含关键词的相同程度,匹配程度越高,越利于关键词排名。又称之为关键词匹配度,是指...

  • 推荐一个终端字体

    Ubuntu终端强烈安利PowerFonts字体,非常好看。 安装步骤 1.终端命令行 2.终端配置 右键配置文件...

  • SH-项目标识替换内容

    项目文件内容替换,根据不同标识等替换对应内容修改国际化Language语言包下.strings文件中内容 终端执行...

  • 003.Ubuntu终端常用的快捷键【Python教程】

    Ubuntu终端常用的快捷键 Ubuntu中的许多操作在终端(Terminal)中十分的快捷,记住一些快捷键的操作...

  • Ubuntu常用命令键

    Ubuntu终端常用的快捷键 Ubuntu中的许多操作在终端(Terminal)中十分的快捷,记住一些快捷键的操作...

网友评论

      本文标题:Ubuntu终端中文件内容的关键词匹配

      本文链接:https://www.haomeiwen.com/subject/zbfaoftx.html