—- 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.
2
Answers
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 –
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:
.
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.