how can i loop through the coming frequency of the keyword

北战南征 提交于 2020-11-25 03:58:08

问题


---- 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 

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

  red hat$
  we$
  hello$
  bye$
  Compensation

----- my code

  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.


回答1:


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

Rewritten for more precise formatting -

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



回答2:


This will produce the expected output based on one interpretation of your requirements and should be easy to modify if I've made any wrong guesses about what you want to do:

$ 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

I assumed your real input doesn't start with a bunch of blanks as in your posted example - if it does that's easy to accommodate.



来源:https://stackoverflow.com/questions/63416009/how-can-i-loop-through-the-coming-frequency-of-the-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!