pattern search between 2 files


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users pattern search between 2 files
# 1  
Old 12-04-2008
pattern search between 2 files

Afternoon guys,

I have 2 files, 1.txt and 2.txt containing employee numbers.
the 1st file (1.txt) is an extract from sybase with active employee numbers, the 2nd (2.txt) is a scan from the sybase log containing successfull logins *** which i have already mined and now contains only employee numbers as well***

I need to find employee numbers that are active but have not logged in, thus i am looking for values withing 1.txt that are not in 2.txt and output this to 3.txt.

I have tried "diff", "comm", awk and a for loop below without success.

#! /bin/sh
awk '
FILENAME=="2.txt" { arr[$0]++}
FILENAME=="1.txt" { if (arr[$0]==0) {print $0} }'
2.txt 1.txt 3.txt

for x in 'cat 1.txt'; do
grep $x 2.txt > test.txt ;
done


Hopefully you guys could please help me out.
# 2  
Old 12-04-2008
Try this:

Code:
awk 'NR==FNR{a[$0];next}!($0 in a)' 2.txt 1.txt > 3.txt

Regards
# 3  
Old 12-04-2008
Quote:
Originally Posted by Franklin52
Try this:

Code:
awk 'NR==FNR{a[$0];next}!($0 in a)' 2.txt 1.txt > 3.txt

Regards
awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 12-04-2008
Quote:
Originally Posted by Jefferson333
awk: syntax error near line 1
awk: bailing out near line 1
Use nawk or /usr/xpg4/bin/awk on Solaris.

If you have GNU AWK and you're missing the above error, try:

Code:
gawk --nostalgia

Smilie
# 5  
Old 12-04-2008
Cool stuff, Thank you guys, much appreciated!
# 6  
Old 12-04-2008
Hammer & Screwdriver Maybe an even simpler solution

Code:
> cat file95
101
102
103
104
105
106
107
108
109
110
> cat file96
101
105
106
107
109

This command will give the entries unique to file95
Code:
> comm -23 file95 file96
102
103
104
108
110

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Pattern search multiple files

#!/usr/bin/ksh a="Run successfully" cd $APPS ls -l *.txt | while read $txt do if then cp $APPS/$txt cp $hist/$txt else rm $APPS/$txt echo "Files has been removed" fi done New in shell script please help me out Around 100 txt files in $APPS dir i want to search pattern from... (8 Replies)
Discussion started by: Kalia
8 Replies

3. Shell Programming and Scripting

Search pattern in today's files only

Hi Friends, I am in search of unix command which can search a particular pattern in all files which are created/modified today ONLY. Which is the best way to achieve this? Thanks in advance. (1 Reply)
Discussion started by: Nakul_sh
1 Replies

4. UNIX for Dummies Questions & Answers

search a pattern across all file and replace it to all files.

Hi All, HP-UX oradev3 B.11.11 U 9000/800 I have few files, which contain texts. Now I need to search a pattern across all files and replace to that pattern with new one. For example, I have following files they have, which contain something like abc@abc.com, now I need to search text acorss... (6 Replies)
Discussion started by: alok.behria
6 Replies

5. Shell Programming and Scripting

Help merging two files if search pattern true.

Hello everyone, I've been reading this forum whenever I had a problem with AWK but I can't seem to find how to solve my problem. What I would like to do is the following: I have a first file with two columns, on the first one is a certain name and in the second one, another corresponding... (4 Replies)
Discussion started by: Teroc
4 Replies

6. Shell Programming and Scripting

pattern search for multiple log files and counting

I have 10 appservers and each appserver has 4 jvms . Each of these logs is archived and stored on a nfs directory . For example the files are /logs/200907/ap1-jvm1.server.log.20090715.gz /logs/200907/ap2-jvm2.server.log.20090714.gz /logs/200908/ap1-jvm1.server.log.20090812.gz I want to... (3 Replies)
Discussion started by: gubbu
3 Replies

7. Shell Programming and Scripting

search files which doesnot match pattern ?

Hi I need a command to search files in a directory which does not match with pattern .. Plz send me this Ex : Test is directory and has some 10 files with different name all are with *.dat extension , need to search files which doesnot contain word "Dummy file". Thanks (6 Replies)
Discussion started by: madankumar
6 Replies

8. UNIX for Dummies Questions & Answers

search all files for a pattern

Hi there, I am looking for a shell script which recursively searches all the files under all the directories for a pattern specified in the script. For e.g., i am looking forward to search for the file names which contains numbers of the form 001*****. Thanks in advance, Naik (4 Replies)
Discussion started by: ynaik002
4 Replies

9. UNIX for Dummies Questions & Answers

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (7 Replies)
Discussion started by: olapxpert
7 Replies

10. IP Networking

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (1 Reply)
Discussion started by: olapxpert
1 Replies
Login or Register to Ask a Question