Skip to main content
added 858 characters in body
Source Link
Kamaraj
  • 4.4k
  • 1
  • 15
  • 19

output 1: Try this code. if needed make adjustments

bash-4.1$ cat test.sh #!/bin/bash OUTPUT_FILE=/tmp/output.txt awk '{  for(i=1;i<=NF;i++)  { Arr[$i]++  }  }  END{  for (i in Arr){ if(Arr[i]>1) { print i":"Arr[i] }  }  }' file* > ${OUTPUT_FILE} cat ${OUTPUT_FILE} echo "" IFS=":" while read WORD TOTAL_COUNT do echo "${WORD}:" for FILE_NAME in file*  do COUNT=$(tr ' ' '\n' < ${FILE_NAME} | grep -c "${WORD}") if [ "${COUNT}" -gt "0" ] then echo "${FILE_NAME}:${COUNT}" fi done done < ${OUTPUT_FILE} bash-4.1$ bash test.sh beautiful:3 so:3 beautiful: file1:1 file2:1 file3:1 so: file1:1 file2:1 file3:1 

output 1:

bash-4.1$ awk '{for(i=1;i<=NF;i++){Arr[$i]++}}END{for (i in Arr){if(Arr[i]>1){print i":"Arr[i]}}}' file* beautiful:3 so:3 

Try this code. if needed make adjustments

bash-4.1$ cat test.sh #!/bin/bash OUTPUT_FILE=/tmp/output.txt awk '{  for(i=1;i<=NF;i++)  { Arr[$i]++  }  }  END{  for (i in Arr){ if(Arr[i]>1) { print i":"Arr[i] }  }  }' file* > ${OUTPUT_FILE} cat ${OUTPUT_FILE} echo "" IFS=":" while read WORD TOTAL_COUNT do echo "${WORD}:" for FILE_NAME in file*  do COUNT=$(tr ' ' '\n' < ${FILE_NAME} | grep -c "${WORD}") if [ "${COUNT}" -gt "0" ] then echo "${FILE_NAME}:${COUNT}" fi done done < ${OUTPUT_FILE} bash-4.1$ bash test.sh beautiful:3 so:3 beautiful: file1:1 file2:1 file3:1 so: file1:1 file2:1 file3:1 
Source Link
Kamaraj
  • 4.4k
  • 1
  • 15
  • 19

output 1:

bash-4.1$ awk '{for(i=1;i<=NF;i++){Arr[$i]++}}END{for (i in Arr){if(Arr[i]>1){print i":"Arr[i]}}}' file* beautiful:3 so:3