我如何遍历关键字的出现频率 [英] how can i loop through the coming frequency of the keyword

查看:93
本文介绍了我如何遍历关键字的出现频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

----我的文本文件,我必须从中搜索关键字[文件名---测试]< cat -Evt文件>

---- my text file from which i have to search for the keywords [name of the file --- test] <cat -Evt file>

    centos is my bro$
    red hat is my course$
    ubuntu is my OS$
    fqdn is stupid $
    $
    $
    $
    tom outsmart jerry$
    red hat is my boy$
    jerry is samall 

------关键字文件为[word.txt]< cat -Evt文件>

------ keyword file is [word.txt] <cat -Evt file >

  red hat$
  we$
  hello$
  bye$
  Compensation

-----我的代码

  while read "p"; do
  paste  -d',' <(echo -n  "$p" ) <(echo "searchall") <(  grep -i "$p" test | wc -l) <(grep  -i -A 1  -B 1   "$p" test )
  done <word.txt
   

----我的期望是,输出应该是

---- my expectation ,output should be

 keyword,serchall,frequency,line above it
                            line it find keyword in
                            line below  it
              
 red hat,searchall,2,centos is my bro
                     red hat is my course
                     ubuntu is my OS                                
            
 red hat,searchall,2,tom outsmart jerry
                     red hat is my boy
                     jerry is samall

----但是从我的代码中输出了

---- but coming OUTPUT from my code

  red hat,searchall,2,centos is my bro
  ,,,red hat is my course
  ,,,ubuntu is my OS
  ,,,--
  ,,,tom outsmart jerry
  ,,,red hat is my boy
  ,,,jerry is samall

----请给我建议,并指出正确的方向,以获得所需的输出.

---- please give me suggestion and point me in the right direction to get the desired output.

----我正在尝试从文件中复制关键字并打印它们 这里应该创建两个记录,因为关键字(红色帽子)要两次出现了

---- i am trying to grep the keyword from the file and printing them Here two records should create as keyword (red hat) is coming two time

----我如何遍历关键字的出现频率.

----how can i loop through the coming frequency of the keyword.

推荐答案

这听起来很像是一项家庭作业.
c.f. BashFAQ 以获得更好的阅读效果;保持简单,专注于您的要求.

This sounds very much like a homework assignment.
c.f. BashFAQ for better reads; keeping this simple to focus on what you asked for.

重写为更精确的格式-

while read key                          # read each search key 
do cnt=$(grep "$key" test|wc -l)        # count the hits
   pad="$key,searchall,$cnt,"           # build the "header" fields
   while read line                      # read the input from grep
   do if [[ "$line" =~ ^-- ]]           # treat hits separately
      then pad="$key,searchall,$cnt,"   # reset the "header"
           echo                         # add the blank line
           continue                     # skip to next line of data
      fi
      echo "$pad$line"                  # echo "header" and data
      pad="${pad//?/ }"                 # convert header to spacving
   done < <( grep -B1 -A1 "$key" test ) # pull hits for this key
   echo                                 # add blank lines between
done < word.txt                         # set stdin for the outer read                       

$: cat word.txt
course
red hat

$: ./tst
course,searchall,1,centos is my bro
                   red hat is my course
                   ubuntu is my OS

red hat,searchall,2,centos is my bro
                    red hat is my course
                    ubuntu is my OS

red hat,searchall,2,tom outsmart jerry
                    red hat is my boy
                    jerry is samall

这篇关于我如何遍历关键字的出现频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆