【问题标题】:how can i loop through the coming frequency of the keyword我如何遍历关键字的出现频率
【发布时间】:2020-12-08 10:20:42
【问题描述】:

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

    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]

  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
   

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

 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

---- 但来自我的代码的输出

  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

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

---- 我正在尝试从文件中 grep 关键字并打印它们 这里应该创建两条记录,因为关键字(red hat)来了两次

----如何循环访问关键字的出现频率。

【问题讨论】:

  • 如果red hat 出现在输入文件的两行中,输出会是什么?如果它根本没有出现怎么办?如果red hate 存在于您的输入文件中,它是否应该与您的“关键字”文件中的red hat 匹配?换句话说,您是要进行部分匹配还是完全匹配?另外,您是否尝试正则表达式或文字字符串匹配?
  • @EdMorton 是的,先生,您是对的,在我的输入文件中,关键字在 2 行中背靠背出现,这是一个问题。第二,我是编码新手和 bash,您可以提供 cmets 或参考给出代码,以便我能理解它是如何工作的先生
  • 那没有回答我的问题。请尝试自己弄清楚我的代码,因为它非常简单(阅读手册页,添加打印语句以查看哪些变量具有哪些值等),如果您有任何具体问题,请在我的回答下提问。

标签: linux bash shell loops grep


【解决方案1】:

这听起来很像家庭作业。
参考文献BashFAQ 以获得更好的阅读体验;保持简单,专注于您的要求。

为更精确的格式重写 -

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

【讨论】:

  • 一开始您的grep 解决方案是正确的。完成它所需要做的就是计算正确输出模 3 的行数,然后 printf 格式化结果。 +1
  • 仍然需要读取文件中的密钥并将其格式化...
  • @PaulHodges 感谢您的回复和时间,但这不是预期的输出
  • 只需要添加一个检查分隔符和一些间隔符echos。您根本没有尝试编辑它吗?
  • @PaulHodges 先生,非常感谢您的帮助
【解决方案2】:

这将根据您对需求的一种解释产生预期的输出,如果我对您想要做什么做出了错误的猜测,应该很容易修改:

$ cat tst.awk
BEGIN {
    RS = ""
    FS = "\n"
}
{ gsub(/^[[:space:]]+|[[:space:]]+$/,"") }
NR == FNR {
    words[$0]
    next
}
{
    for (word in words) {
        for (i=1; i<=NF; i++) {
            if ($i ~ word) {
                map[word,++cnt[word]] = (i>1 ? $(i-1) : "") FS $i FS $(i+1)
            }
        }
    }
}
END {
    for (word in words) {
        for (i=1; i<=cnt[word]; i++) {
            beg = sprintf("%s,searchall,%d,", word, cnt[word])
            split(map[word,i],lines)
            for (j=1; j in lines; j++) {
                print beg lines[j]
                beg = sprintf("%*s",length(beg),"")
            }
            print ""
        }
    }
}

.

$ awk -f tst.awk words file
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

我假设您的实际输入不会像您发布的示例中那样以一堆空白开头 - 如果这样做很容易适应。

【讨论】:

  • 先生,这段代码没有运行,我正在运行相同的命令 awk -f words file , contains keywords and contains text
  • 我在以我展示的方式运行时发布的脚本无法做到这一点,所以你做错了什么,但我看不到你的桌面是什么。您是完全使用您在问题中提供的示例输入还是针对某些不同的输入集来运行它? doesn't run 是什么意思?它是否执行但不产生输出,或者进程是否像等待输入一样挂起?如果在每个 { 之后的行中添加 print 语句会发生什么?
  • 先生它执行( awk -f tst.awk 单词文件)但不打印任何内容,我的两个文件是相同的测试-内容和单词在哪里-关键字在哪里
  • 先生,我已经添加了图片
  • 我修复了一个错误,该错误会导致输出全部显示在 1 行而不是 3 行上,但这不会导致您的问题。从您发布的图片(请不要发布图片 - 发布文本)来看,它看起来背景中的脚本与我的相同,所以唯一的方法是您不会获得任何输出如果您的单词文件中的任何“单词”都不匹配您其他文件中的任何行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 2021-10-16
  • 2021-08-04
  • 2020-06-01
  • 2023-01-20
  • 1970-01-01
相关资源
最近更新 更多