Problem using Grep for a loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem using Grep for a loop
# 1  
Old 01-23-2014
Question Problem using Grep for a loop

Dear All,
I have problem with generalizing my code and I can't see where the problem is.

I have a main.txt file that has all the information and then I have 4 folders (headoffice, branch, management, office) that each have a 2 of files keep.txt and throw.txt and each of them have few lines (so 4 keep and 4 throw)

I wrote a line that goes throw each line a main.txt and it creates a file with only lines that has an object name that is on the keep and not on the throw for the headoffice

Code:
grep -F -f headoffice/keep.txt main.txt | grep -v -F -f headoffice/throw | sort -u > result/headoffice-keep.txt

my problem is when I want to create a loop around it to take all the folders together as it gives me the wrong answer and I can't see why... tried millions of ways and one of them is the following

Code:
mkdir result		
FILES="folder/*/*"
for W in $FILES
do
	doc=$(basename $W) 
	mkdir result/${doc}
		grep -F -f */*/keep.txt main.txt | grep -v -F -f */*/throw.txt | sort -u > result/${doc}/${doc}-keep.txt		
done

Can someone help me plzSmilie
# 2  
Old 01-23-2014
I take it then that $FILES contains directories that contain a keep.txt file and a throw.txt file? If so try:
Code:
grep -Ff "$W/keep.txt" main.txt | grep -vFf "$W/throw.txt" | sort -u > "result/${doc}/${doc}-keep.txt"

# 3  
Old 01-23-2014
For each W you certainly want to consider one keep.txt and not all */*/keep.txt? (Dito throw.txt.)
Suggestion:
Code:
...
    doc=$(basename "$W")
    docpath=$(dirname "$W")
    mkdir result/"$doc"
    fgrep -f "$docpath"/keep.txt main.txt | fgrep -v -f "$docpath"/throw.txt | sort -u > result/"$doc/$doc"-keep.txt
...

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 01-23-2014
thank you very much
I have changed few things as I wanted the results to be in a similar shaped folders (with sample folder names) e.g.
Code:
	mkdir result/${docpath}

just tested on two folders and hope it will work well when i put the rest of the files in
 
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 Dummies Questions & Answers

Problem with multiple grep in bash loop

Hello, I am trying to create a matrix of 0's and 1's depending on whether a gene and sample name are found in the same line in a file called results.txt. An example of the results.txt file is (tab-delimited): Sample1 Gene1 ## Gene2 ## Sample2 Gene2 ## Gene 4 ## Sample3 Gene3 ... (2 Replies)
Discussion started by: InfoSeeker2
2 Replies

3. Homework & Coursework Questions

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... (7 Replies)
Discussion started by: jcllns1
7 Replies

4. 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

5. 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

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

Problem in getting data from a loop using grep and cut

The script is following : for each_rec in <file_name> do count=`cut -c -2 ${each_rec} | grep "45"` echo ${count} if ] then amount=`cut -c 24-35 ${each_rec}` echo ${amount} else echo "failed" fi done And the file looks like below : ... (4 Replies)
Discussion started by: mady135
4 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