I'm basicly trying to write a script that will search through all files in the /home directory and sorting out key words such as kill, bomb, porn ... etc. Then having the output displayed as
User "username" has "the keyword" in "filename"
I'm pretty new to this and I'm pretty stuck
This is what I have so far

for X in kill bomb porn 
do 
USERN=`grep –i –r “$X /home | cut –d “/ –f 3`
WORD=`grep –i –o –r “$X /home | cut –d “: –f 2`
FILE=`grep –i –r “$X /home | cut –d “: –f 1`
Echo “User $USERN has $WORD in $FILE
done

this works fine if the key word comes up in only one file, but if it comes up in multiple files the output comes out like
User blah
jenkins has kill
kill in /home/blah/mydocuments/mytext
/home/jenkins/mydocuments/testtxt
(blah and jenkins are my two users)
thats what happens when kill shows up in two files
any suggestions of how to fix that?

I was messing around with it some more and came up with this

for X in kill bomb porn
do 
USERN=`grep -i -r "$X" /home | cut -d "/" -f 3`
FILE=`grep -i -r "$X" /home | cut -d ":" -f 1`
echo "User "$USERN" has the word "$X" in "$FILE"."
done

Problem is this way puts both users on the same line and each file on the same line also.
output is like this
User blah jenkins has the word kill in /home/blah/mydocuments/mytext /home/jenkins/mydocuments/test

I guess what i'm looking for is a way to seperate them.
so it looks like
User blah has the word kill in /home/blah/mydocuments/mytext
User jenkins has the word kill in /home/jenkins/mydocuments/test

This article has been dead for over six months. Start a new discussion instead.