Problem writing a search script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem writing a search script
# 1  
Old 10-29-2011
Problem writing a search script

I trying to write a script in bash that take a list of users in “fileA” and searches another list user in “fileB” if the user does not exist in “file B” write the user to another file “file C”.

Please note “fileA” and “fileB” contains over 1000 users

Basically

Code:
“fileA”          “fileB”

Abc                 Tom
Def                 John
Tom                 Jonathan
Paul                 Paul
Wood              Graham

Result in FileC
Code:
Abc
Def
Wood

Here my attempt

Code:
for i in `cat fileA`; do grep –w $i fileB; test [$? –ne 0] && echo $i >fileC  fi; done

Can you help please my attempt is not working?
Thanks
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-29-2011 at 06:32 PM.. Reason: code tags, please!
# 2  
Old 10-29-2011
Code:
nawk 'FNR==NR{fB[$0];next} !($0 in fB)' fileB fileA > fileC

# 3  
Old 10-31-2011
Hassan,

Your code looks fine except for below error:

Code:
 
for i in `cat fileA`; do grep -w $i fileB; test [$? -ne 0] && echo $i >>fileC  fi; done

You need ">>" before fileC so that the users are appended. Else only the last user in file1 that is not present in file2 would be present in file3.

Thanks,
Vijay V Menon
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Writing a search, copy and paste program

Hello, Can some one help me by writing me the script of a program that does the following simple functions on a Linux Mint: 1. it runs in the background all the time, doing nothing except of checking if there is any external device mounted. 2. when the device is detected, it copies files... (1 Reply)
Discussion started by: Black_Ardilla
1 Replies

2. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

3. Programming

Binary Search Tree Search problem

I am writing code for a binary search tree search and when I compile it i am getting strange errors such as, " /tmp/ccJ4X8Xu.o: In function `btree::btree()': project1.cpp:(.text+0x0): multiple definition of `btree::btree()' " What does that mean exactly? tree.h #ifndef TREE_H #define... (1 Reply)
Discussion started by: meredith1990
1 Replies

4. Shell Programming and Scripting

Problem with writing into a file through shell script

Hi all, I have a shell script which I use to login to the server from the client and then from the server I run a bunch of other scripts to complete my task. I am having problems with the script below- #!/bin/bash while read line do connections=`echo $line | cut -d " " -f 1` period=`echo... (3 Replies)
Discussion started by: joydeep4u
3 Replies

5. Shell Programming and Scripting

help writing a scipt to search for errors.

Hi, I am a beginner unix user. I would appreciate any help you guys can provide me with. What I am looking to do is the following. I have a log file that gets generated every morning. Example: /home/me/folder/temp.log temp.log will say "Socket connected" if the connection was successful,... (4 Replies)
Discussion started by: Jeffenri
4 Replies

6. Shell Programming and Scripting

Problem writing to different files

Hello: I have the following code: ---------------------------------- open (OUTPUT_FILE, ">>/usr/users/rovolis/PREPAID/CC/TCG/PP.$cyear$cmonth$cday.txt")||die "$!"; 82 open (OUTPUT_FILE2, ">>/usr/users/rovolis/PREPAID/CC/TCG/PR.$cyear$cmonth$cday.txt")||die "$!"; 83 # ... (0 Replies)
Discussion started by: chriss_58
0 Replies

7. Shell Programming and Scripting

problem writing a simple c shell script

#!/bin/csh echo hello world this is what i got in a text file called ss1. i type "chmod 755 ss1.txt" to make it executable. then when i type ss1 or ss1.txt it says "ss1 command not found" what am i doing wrong? (19 Replies)
Discussion started by: pantelis
19 Replies

8. Shell Programming and Scripting

Script writing problem

Self professed idot looking for help LOL Hi all, I am new to Unix and I have to write a shell script that will check to see if a file exist and then create it if it does not. The file I need to search for is titled "A1. dat" and here is my feeble attempt at creating the script: #!/bin/bash... (2 Replies)
Discussion started by: Tinablue
2 Replies

9. UNIX for Advanced & Expert Users

Problem while writing to a file...?

Hi, I have an issue with the file writing... It is, Suppose that if I am writing some data to a file... and at the same time another user has opened the file and want to write in to the file(writing to the file at the same time)...the another has to know that someone has opened the file, mean... (3 Replies)
Discussion started by: vijay4b7
3 Replies

10. UNIX for Dummies Questions & Answers

Problem with writing a program

Hi guys I'm having trouble with trying to create a script which calculates the grade of a student and the marks out of 300. The grades are: 0-49% fail 50-59% pass 60-69% credit pass 70-79% distinction 80-100% high distinction less than 0 or greater than 100 displays error message. My... (1 Reply)
Discussion started by: CompNoob
1 Replies
Login or Register to Ask a Question