-1

i need to write a shell script to find the content of one file[all the content of this file] into another file along side it displays the line number and total number of occurrence of the content as an output .

#!/bin/ksh
file="/home/ashish/contents.txt"

while read -r line; 
do
    grep $line /home/ashish/first.csv
done < "$file"

for example : first file contain this code "at java.lang.Object.wait(Native Method) - waiting on <0x272d5c18> (a weblogic.rjvm.ResponseImpl) at weblogic.rjvm.ResponseImpl.waitForData(ResponseImpl.java:76) - locked <0x272d5c18> (a weblogic.rjvm.ResponseImpl) " second file contain code in bulk amount so i need to count the number of occurrence of code in the first file inside second file Output=number of times code in first file occur in second file along with line numbers (if possible).

Ashish Joshi
  • 47
  • 1
  • 7
  • 1
    Please share the expected output – bluefoggy Feb 17 '15 at 07:05
  • bluefoggy: It is perfectly clear what he needs. @Ashish: I would do this in two steps: the first script creates a longer script with grep's for every entry in contents.txt. You can automatically delete it afterwards. – Rob Feb 17 '15 at 07:16
  • let us suppose we have two files as first and second and the content of first and second are as follows first : hello hi second : hi hello my name is ashish he said hi hello to her desired output should be 2// which is the occurance of content if first in second line 1 line 2 which state the line number where it occur – Ashish Joshi Feb 17 '15 at 07:39
  • Please edit your question and show the expected output there. – bluefoggy Feb 17 '15 at 08:04
  • first file contain this code "at java.lang.Object.wait(Native Method) - waiting on <0x272d5c18> (a weblogic.rjvm.ResponseImpl) at weblogic.rjvm.ResponseImpl.waitForData(ResponseImpl.java:76) - locked <0x272d5c18> (a weblogic.rjvm.ResponseImpl) " second file contain code in bulk amount so i need to count the number of occurrence of code in the first file inside second file Output=number of times code in first file occur in second file. – Ashish Joshi Feb 17 '15 at 08:39
  • possible duplicate of [how to show lines in common (reverse diff)?](http://stackoverflow.com/questions/746458/how-to-show-lines-in-common-reverse-diff) – tripleee Feb 17 '15 at 08:54

1 Answers1

0

You can simply grep the contents and there could be repeated entries so at the end just pipe the output to sort -u or sort | uniq

like:

while read -r line;do  echo -n $(grep -c $line /home/ashish/first.csv); echo "  $line";done <"$file" | sort -u

grep -c will count the occurances sort -u will sort the output uniquely

bluefoggy
  • 961
  • 1
  • 9
  • 23
  • i need to search for multiple lines and this is searching for only single line. i mean if i have to search multiple strings into the second file – Ashish Joshi Feb 17 '15 at 07:59