comparing in files and output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing in files and output
# 1  
Old 02-02-2011
comparing in files and output

i have a file a with contents
Code:
tom   
lasole
jon   
gille
sam

and a file b with contents
Code:
tom|1234|abcf|newyork,ohio,oregon
sam|2345|drft|texas,london
hyle|4444|befr|wisconsin
neyo|2333|tdtt|ohio,jersey

i want to compare records in file a to file b such that when the name tom is found in file b column 1 and has either newyork or ohio or oregon IN Column 3 display as
Code:
tom|NOO

else display
Code:
sam|YES.

so the output of file a after comparison should be
Code:
tom | NOO  
lasole|YES
jon|YES   
gille|YES
sam|YES

since tom has either newyork or ohio or oregon in column3 of file b


Moderator's Comments:
Mod Comment You were already asked in another thread to use code tags - you also got a warning PM with a guide how to use them and still don't do so. If you collect 30 points by such tiny things like this, you will be banned from the forum. So please start obeying mod guidances or tell us what you do not understand.

Last edited by zaxxon; 02-02-2011 at 11:47 AM.. Reason: code tags
# 2  
Old 02-02-2011
A simple script should suffice:-
Code:
#!/bin/ksh
for ref in `cat filea`
do
   cities=`grep "^$ref fileb"|cut -f3 -d"|"|tr "," " "`
   match="NOO"
   for city in $cities
   do
      if [ "$city" = "newyork" -o "$city" = "ohio" -o "$city" = "oregon" ]
      then
         match="YES"
      fi
   done
   echo "$ref|$match"
done



I hope that this helps, but your desired output is a little inconcistent, so I've had to guess. have a play and let us all know how you get on or if you need more help.


Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 02-02-2011 at 01:01 PM.. Reason: Closing and signature
# 3  
Old 02-03-2011
its not working
# 4  
Old 02-03-2011
It would appear there is a quote in the wrong place.

Code:
grep "^$ref fileb"

should be
Code:
grep "^$ref" fileb

Output now is:
Code:
tom|NOO
lasole|NOO
jon|NOO
gille|NOO
sam|NOO

If that doesn't solve your problem, please say what doesn't work.

Just saying "It doesn't work" is really no use to anyone.
# 5  
Old 02-03-2011
output should be
Code:
tom | NOO 
lasole|YES
jon|YES 
gille|YES
sam|YES

# 6  
Old 02-04-2011
Many thanks Scott. I am a fool in my coding.Smilie I read the text saying column 3 and just coded it straight. Smilie

So, dealerso, now with proper testing, I found that my logic was in reverse too.

Amended & testing code appended:-
Code:
for ref in `cat filea`
do
   cities=`grep "^$ref" fileb|cut -f4 -d"|"|tr "," " "`
   match="YES"
   for city in $cities
   do
      if [ "$city" = "newyork" -o "$city" = "ohio" -o "$city" = "oregon" ]
      then
         match="NOO"
      fi
   done
   echo "$ref|$match"
done

From the input given, I get the output:-
Code:
tom|NOO
lasole|YES
jon|YES
gille|YES
sam|YES

Not quite what is shown, but that would be more to code, unless you really want the spaces around the pipe mark for record tom.

Does that fix things up? Let us all know if this is the answer or if I have missed something. Smilie



Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Comparing 2 xml files and print the differences only in output

Hi....I'm having 2 xml files, one is having some special characters and another is a clean xml file does not have any special characters. Now I need one audit kind of file which will show me only from which line the special characters have been removed and the special characters. Can you please... (1 Reply)
Discussion started by: Krishanu Saha
1 Replies

3. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

4. Shell Programming and Scripting

Comparing text in 2 files and output difference in another file.

I have 2 files of almost same text apart from 2,3 ending lines. Now I want to get that difference in another file. e.g file1.txt is Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_livecd-lv_root 18G 2.4G 15G 14% / tmpfs 504M ... (12 Replies)
Discussion started by: kashif.live
12 Replies

5. Shell Programming and Scripting

Comparing a text file's entries with ps -ef output

Shell : Korn os : AIX This is the ps output looking for a process called pmon. pmon runs with various 'service' names which is appended with an underscore as shown below. For example a pmon process for the service hexjkm will be named ora_pmon_hexjkm $ ps -ef | grep pmon oracle... (1 Reply)
Discussion started by: kraljic
1 Replies

6. Shell Programming and Scripting

Comparing file with Sql output.

Hi Guys, I need to compare the sql output with a column present in a file. I am able to read the column from the file and stored in a variable. Can somebody help how to store output of the below query to a variable. The database is Oracle. Select count(*) from tableName; ... (2 Replies)
Discussion started by: mac4rfree
2 Replies

7. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

8. Shell Programming and Scripting

comparing output of who command

I have a need to take the output of the who command and first strip out users with only 1 session, then of the remaining users I need only to list the ones who are using more than 1 IP address Normal output: tander01 pts/113 Feb 11 09:21 (10.25.1.23) tbanks pts/81 ... (4 Replies)
Discussion started by: mshilling
4 Replies

9. Shell Programming and Scripting

comparing of two files with output which is not in both files

Hi I am having two files... x1 709545 709546 709547 and x2 709545 709545 709545 the output should be 709546 709547 pls help (2 Replies)
Discussion started by: suryanarayana
2 Replies

10. Shell Programming and Scripting

Find duplicate value comparing 2 files and create an output

I need a perl script which will create an output file after comparing two diff file in a directory path: /export/home/abc/file1 /export/home/abc/file2 File Format: <IP>TAB<DeviceName><TAB>DESCRIPTIONS file1: 10.1.2.1.3<tab>abc123def<tab>xyz.mm1.ppp.... (2 Replies)
Discussion started by: ricky007
2 Replies
Login or Register to Ask a Question