GREP loop


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions GREP loop
# 1  
Old 02-15-2013
GREP loop

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
I need to search through the users home directories for keywords, display them. The code listed below will show me the first result but I am not sure how to loop through the results. I am not sure how to make a loop work.

I then need to exclude users from the search if what that script found was phrase that is acceptable. So if the script is ran again the user will not come up again in the search.

I am using a file with a list of the word to search, could I do this with the exclusions as well?


2. Relevant commands, code, scripts, algorithms:
ECHO
GREP


3. The attempts at a solution (include all code and scripts):

Code:
RESULT=`grep -irf searchwords /home`
FLOC=`echo $RESULT | cut -d : -f 1`
UNAME =`echo $FLOC | cut -d / -f 3`
PHRASE=`echo $RESULT | cut -d : -f 2`
echo "Username: $UNAME, Line with bad word found: $PHRASE, and Path and file name: $FLOC."

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Northeast Wisconsin Techical College, Green Bay, WI - Joe Cicero - UNIX I

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 02-15-2013
How many keywords in the file, how many bytes? Command lines have their limits.
# 3  
Old 02-15-2013
Right now there are only about 6 words in the file
# 4  
Old 02-17-2013
So, turn them into an egrep pattern. Use a 'while read' on the file and concatenate them with pipe marks.
# 5  
Old 02-19-2013
So here is what a fellow student and I came up with, and looks to be working...

Code:
RESULT=($(grep -irf searchwords /home | cut -d / -f3))

for USERLIST in ${RESULT[*]}
do
	CHECKGOOD=`grep $USERLIST /opt/guser`
	if [ "$CHECKGOOD" == "" ]
	then
		PHRASE=`grep -irf searchwords /home/$USERLIST | cut -d : -f2`
		FLOC=`grep -irf searchwords /home/$USERLIST | cut -d : -f1`
		echo "Username: $USERLIST has the following phrase: $PHRASE located at: $FLOC"
		echo "Was this an appropriate use of the phrase?"
		read RESP
		LRESP=`echo $RESP | tr [:upper:] [:lower:] | cut -c 1`
		if [ "$LRESP" == "y" ]
		then
			echo $USERLIST >> guser
		fi
	fi
done

# 6  
Old 02-19-2013
Good so far. You are making some typical beginners errors, though:

Code:
RESULT=($(grep -irf searchwords /home | cut -d / -f3))

The outer "(...)" is superfluous, but the quoting is missing. Whenever a string might contain whitespace in a shell script you should quote it: first, to protect the whitespace from being interpreted away by the shell and second to make clear that the shell should not treat the whitespace as a separator.

Another thing is: you nowhere declare RESULT to be an array, but you treat it as one further down. Even if the shell allows for such liberty you should write your scripts as if it doesn't. In this case the outer braces are necessary but the subshell ("$(...)") isn't. The following should work:

Code:
typeset -a RESULT=(grep -irf searchwords /home | cut -d / -f3)

Code:
for USERLIST in ${RESULT[*]}

This is working but a dangerous way to loop through an array. The shell has a maximum line length (see "limits.h") and works on a lin in this way: first, all the variables are scanned and replaced by their value. Then the resulting line is evaluated. Depending on how big the array RESULT is the line could be pretty long and this could result in the shell rejecting the line with "line too long". Use a while-loop with an index counter instead:

Code:
(( iCnt = 0 ))
while [ $iCnt -lt ${#RESULT[*]} ] ; do
     do-something "${RESULT[$iCnt]}"
     (( iCnt += 1 ))
done

Code:
PHRASE=`grep -irf searchwords /home/$USERLIST | cut -d : -f2`
FLOC=`grep -irf searchwords /home/$USERLIST | cut -d : -f1`

This is possible, but a time-killer. You issue the same command twice, every time using a different part of the output instead of doing it once. Here is a trick: "read" can read in not only one but several variables:

Code:
grep -irf searchwords /home/$USERLIST | cut -d : -f1,2 | read FLOC PHRASE

This will do the same as your two lines above.

Code:
CHECKGOOD=`grep $USERLIST /opt/guser`

You do this - use of backtics - a lot. You shouldn't. backtics are outdated and deprecated and only supported for backwards compatibility reasons. You should use the subshell-command instead:

Code:
var=`command-list`    # wrong and to be avoided
var="$(command-list)"  # correct


I hope this helps.

bakunin

Last edited by bakunin; 02-19-2013 at 08:20 PM..
This User Gave Thanks to bakunin For This Post:
# 7  
Old 02-20-2013
You do not need " around $(...) in a simple variable assign, as it is already implicitly quoted and will all end up in your variable in one piece. In other situations, you might need that because the shell gives it a second look, but not simple variable assignment.

The indexes can be generated at point of use using $(( iCnt++ )) in bash.

You can have pipeline parallelism and no array variable with a simple pipe:
Code:
grep -irf searchwords /home | cut -d / -f3 | while read USERLIST
 
for:
 
RESULT=($(grep -irf searchwords /home | cut -d / -f3))

for USERLIST in ${RESULT[*]}


Last edited by DGPickett; 02-20-2013 at 03:17 PM..
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ps ax with grep in loop

Hello, I have built the following script to check if processes supplied by the argument are running or not. #!/bin/bash PROCLIST=$1 PROCESS="0" ERROR_PROCS="" IFS='+' read -ra ADDR <<< "$PROCLIST" for PROC in "${ADDR}"; do if ; then PROCESS=1 ... (9 Replies)
Discussion started by: nms
9 Replies

2. UNIX for Beginners Questions & Answers

Grep command in a loop

Hello - I am running a script that is outputting to a log. Let call it output.log I would like to monitor that log until the line "Build Successful" is found. I think I would need to use the grep command. How would I do that in a loop? Thanks Marty (1 Reply)
Discussion started by: MSpeare
1 Replies

3. Red Hat

using grep in a while loop

Hello everybody, I have been searching it, but it seems I am unable to find the correct information, that s why I am asking you guys, hoping somebody get an idea. Here is my problem : I want a script to loop until a string is identified in a log file. Here is the script : #!/bin/sh... (5 Replies)
Discussion started by: guyiom
5 Replies

4. Shell Programming and Scripting

Help on grep in a do while loop

So this is what I'm trying to do: I have a file called registry.txt which has a list of registry entries I want to search for. I have another file called inctrl.txt on which I want to perform the search on. Here's the example contents of registry.txt SOFTWARE\Microsoft\Security... (3 Replies)
Discussion started by: r4v3n
3 Replies

5. UNIX for Dummies Questions & Answers

grep sed and a loop

:wall: I have a requirement to search a log file that never rotates for certain values. If I find them I pipe them to a another file. To log file is constanyl being appened with new lines and never rotating Easy so far. The problem is I dont want to pipe out matches already seen before. ... (3 Replies)
Discussion started by: gunnahafta
3 Replies

6. Shell Programming and Scripting

Using grep within a while loop

Hi all, I have the below script to get input but i cannot get grep to work. input1.txt AAAAAAAAG input2.txt >gi|184009.1| LEAFY-like |AAAAAAAAGSGGGDHLPY However, when i use grep -f input1.txt input2.txt i cannot get any output matches (note that the match is underlined). Is it... (8 Replies)
Discussion started by: turkishvan
8 Replies

7. Shell Programming and Scripting

Help with grep inside an if loop

Hello All, I have been reading posts on here for a while, but this is my first post. I have a document in which many sentences appear, and I am piping it through an exterior script which will tag each word in the document with its part of speech (not part of my script, just background). The... (3 Replies)
Discussion started by: daf189
3 Replies

8. Shell Programming and Scripting

Grep commands in loop

Hi All, Reference to my previous post I need to compare all the lines in the file1 with file2 for this condition if file1 {$3,$5} ==file2 {$3,$5} then grep file2{$1}latest date. need output in file3 10/04/2008 09/04/2008 09/04/2008 08/04/2008 can anyone suggest me Thanks... (0 Replies)
Discussion started by: karthikn7974
0 Replies

9. UNIX for Dummies Questions & Answers

grep -v while loop

alist contain: a b c d e blist contain: a b c the code: #!/usr/bin/ksh cat blist | while read line do grep -V "$line" alist > data done (8 Replies)
Discussion started by: bobo
8 Replies

10. Shell Programming and Scripting

grep in a loop

Hi , I am trying a script which takes user input userid . I am stuck how to check whether that is a valid user id or not in the audit log files. My code is : cd $CCP_AUDIT cat * > /export/home/$USR/l***/files echo "UserId:\c" read UserId #Date Function echo "DATE : \c" read xxx I... (7 Replies)
Discussion started by: gundu
7 Replies
Login or Register to Ask a Question