How can I set up this loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I set up this loop?
# 1  
Old 10-02-2012
How can I set up this loop?

I want to compare two files. I think using grep is the way to go but I do not know how to set this up. Let me show an example:
File1 has 1000 items, File2 has 700 items
I need to take the first item in File1 and search the entire contents of File2 to see if there is a match there. IF there is a match (meaning the same item is in both File1 and File2) then do nothing. IF NOT send that item to an output file named testresults. Something like this:

Open File1,take first variable1
search variable1 in all of File2,
IF variable1 is in File2, do nothing
IF NOT, send variable1 to testresults
Continue loop until all of the items in File1 have been searched for in File2

I thought of using diff and sort, but the problem is that there are much more items in File1 than in File2 which will throw off the results. Another solution might possibly be to use diff and sort in a loop as well? Any help would be appreciated but I believe grep is the easiest way, I could be wrong though

---------- Post updated at 01:30 PM ---------- Previous update was at 01:25 PM ----------

This is what I tried doing, and the weird thing about it is that it seems to work (when I open up testresults, it shows me which files are in file1 that are not in file2), however when i run the code, the command prompt prints out the items that are a match in both files. Why is it doing this?

Code:
for i in `cat file1`
do
grep $i file2
if [ ! $? -eq 0 ]
then
{
echo $i >> testresults
}
fi
done




Also tried this (someone suggested using this) but results came out blank... not sure why...

Code:
(
awk -F"/n" '
BEGIN {
while( getline < "'file2'" )
{ arr[file1]=1 }
}
arr[file1] != 1 { print }
' file1
) > testresults2


Last edited by Corona688; 10-02-2012 at 07:34 PM..
# 2  
Old 10-02-2012
please provide sample input files and the desired output.
please use code tags when posting data/code samples.
# 3  
Old 10-02-2012
Example:

file1:
Code:
oranges
apple
water
time
machine

file2:
Code:
water
ryan
king
apple
oranges

Desired output:
Code:
time
machine

output that is displaying on prompt:
Code:
oranges
apple
water

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-02-2012 at 03:48 PM.. Reason: code tags, please (as asked before)
# 4  
Old 10-02-2012
Code:
awk 'FNR==NR{f2[$1];next} !($1 in f2)' file2 file1

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to set Global variable outside while loop

Below is my code: count=0 if ... ... else ... find * -prune -type d | sort -r -n | while read d; do count=1 if ; then echo "Count1:$count" ... ... break 2; fi ... done ... fi echo "Count2:$count" (9 Replies)
Discussion started by: mohtashims
9 Replies

2. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

3. Shell Programming and Scripting

use variable to set the range of a for loop

Hi; For sure there's an easy answer to this one that I am not finding.. I first set a variable, say b1a:] max=5 then I want to use max to set the range for a for loop like so (it should run for i in 1:5) b1a:] for i in {1..$max}; do echo $i; done {1..5} I would like the output... (2 Replies)
Discussion started by: jbr950
2 Replies

4. Shell Programming and Scripting

Set variables from file, then loop

Hello, I am very, very new to shell scripting, but what I'm attempting to do is read in a list of user ID's to create on a database system from a CSV flat file, and for each entry run the "create user" script. I've gotten pretty far but I'm having trouble with the looping mechanism.... Any... (8 Replies)
Discussion started by: jkarren
8 Replies

5. Shell Programming and Scripting

set -options not working inside for loop?

I'm a beginner in shell scripting (I'm using ksh). I'm manipulating some files and I'm using set -A to transform each read line into a numeric array. However, inside the 'for' loop the options of set (ie '-A') are not recognized (the vi editor doesn't highlight it and it doesn't work). Where... (4 Replies)
Discussion started by: kasumlolla
4 Replies

6. UNIX for Dummies Questions & Answers

Loop through Sub Directories and search for set of files

I have the below directory in unix environment /home/bkup/daily: ls -lrt drwxrwx--x 2 user user 256 Jan 12 18:21 20110112/ drwxrwx--x 2 user user 256 Jan 13 17:06 20110113/ drwxrwx--x 2 user user 256 Jan 14 16:44 20110114/ drwxrwx--x 2 user user ... (2 Replies)
Discussion started by: prasannarajesh
2 Replies

7. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

8. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

9. Shell Programming and Scripting

Keeping vars set in while loop with redirection

Under IRIX 6.5, the Bourne shell is named /bin/bsh. I need to redirect output into a file reading loop, and retain values set within the loop based on processing that goes on within the loop for later processing. This code #!/bin/bsh k=1 cat file | while read k ; do echo "$k" done... (2 Replies)
Discussion started by: criglerj
2 Replies
Login or Register to Ask a Question