Sponsored Content
Top Forums Shell Programming and Scripting comparing 2 files and creating third file with uncommon content Post 302598330 by ajaypatil_am on Tuesday 14th of February 2012 05:48:59 AM
Old 02-14-2012
sorry if i was not clear ...the both the files will contain only one 'ajay' only one 'suhas' but there can be thousand of such names...
all i want is the thrid file with names which are there in file 2 but not in file1..

Quote:
Originally Posted by birei
Try:
Code:
$ cat file1
ajay suhas tom nisha vijay mahish
ajay suhas tom nisha vijay mahish
ajay suhas tom nisha vijay mahish
ajay suhas tom nisha vijay mahish
$ cat file2
ajay suhas tom nisha
ajay suhas tom
tom
tom nisha
$ cat script.pl
use warnings;
no strict qw(refs);
 
die qq[Usage: perl $0 <file1> <file2>\n] unless @ARGV == 2;
 
open my $fh1, qq[<], shift or die qq[Cannot open input file: $!\n];
open my $fh2, qq[<], shift or die qq[Cannot open input file: $!\n];
 
while ( my $l1 = <$fh1>, my $l2 = <$fh2> ) {
        chomp( $l1, $l2 );
        my %h1 = map { $_ => 1 } split /\s+/, $l1;
        delete @h1{ split( /\s+/, $l2 ) };
        printf qq[%s\n], join qq[ ], keys %h1;
}
$ perl script.pl file1 file2
vijay mahish
vijay mahish nisha
suhas vijay mahish nisha ajay
suhas vijay mahish ajay

Regards,
Birei
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating a tabulated file from content of 2 or more files

Hi all I need specific fields from a file to be tabulated into a single file. I have a file with the following :- FIELD1 InfoA FIELD2 InfoB FILED3 InfoC FIELD1 InfoD FIELD2 InfoE FIELD3 InfoF The output... (2 Replies)
Discussion started by: jhansrod
2 Replies

2. Shell Programming and Scripting

comparing file content differences

I need to write a script to find out if there are any .c files created/removed from the last time i monitored the files available. i first created a file to contain all the .c files available on the system. (ls *.c > file1) I created another file using the same command. I used the comm file1... (4 Replies)
Discussion started by: RianTan
4 Replies

3. Shell Programming and Scripting

comparing files content

hi i have a set of files , i need to compare one file content with other file content, i am using cmp -s abc.1 def.2 , but it is not giving theproper o/p even if the content is different.Please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

4. Shell Programming and Scripting

Comparing 2 csv files and matching content

Hello, I have the following problem: There are two csv files csv-file #1: aaa1, aaa2, ... aaan aaa1, bbb2, ... bbbn aaa1, ccc2, ... cccn bbb1, bbb2, ... bbbn ... zzz1, zzz2, ... zzzn csv-file #2: aaa1, matchvalue1 ccc1, matchvalue2 (7 Replies)
Discussion started by: ghl10000
7 Replies

5. UNIX for Dummies Questions & Answers

UNcommon lines between files

I have two files, file1.txt and file2.txt I want to find UNcommon between two files with while read line do grep -v $line file2.txt done<file1.txt I get the reverse grep for every line it reads (7 Replies)
Discussion started by: FelipeAd
7 Replies

6. UNIX for Dummies Questions & Answers

creating text file with content from script

hi, can somebody tell me how I can create a text file with content from Bash script. The file should be prefilled with information such as current date and time then leaving the user ability to input more data right below those prefilled content. thank you :) (0 Replies)
Discussion started by: s3270226
0 Replies

7. Shell Programming and Scripting

Comparing two files and creating a new file

Hi, I want to compare two files based on the data in their first column. Both the files are not equal in their number of columns and number of entries or rows. The entries (only the first column) of the file1 should be compared with the entries (only the first column) of the file2. If the... (3 Replies)
Discussion started by: begin_shell
3 Replies

8. Shell Programming and Scripting

Comparing file ownership/permission and content of files located on two different servers

Hi All, can some one suggest me a tool to compare file ownership/permission and contents of files located at two different unix servers? Thanks, Pranav (1 Reply)
Discussion started by: Pranav Bhasker
1 Replies

9. Shell Programming and Scripting

From 2 files create 3rd file with uncommon data

Hi All, I have two files. File1 and File2. Want to create another file with all the records of File1 those are not present in File2. Please guide. Thanks in advanced. Anupam (3 Replies)
Discussion started by: Anupam_Halder
3 Replies

10. 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
DIFF3(1)						      General Commands Manual							  DIFF3(1)

NAME
diff3 - 3-way differential file comparison SYNOPSIS
diff3 [ -exEX3 ] file1 file2 file3 DESCRIPTION
Diff3 compares three versions of a file, and publishes disagreeing ranges of text flagged with these codes: ==== all three files differ ====1 file1 is different ====2 file2 is different ====3 file3 is different The type of change suffered in converting a given range of a given file to some other is indicated in one of these ways: f : n1 a Text is to be appended after line number n1 in file f, where f = 1, 2, or 3. f : n1 , n2 c Text is to be changed in the range line n1 to line n2. If n1 = n2, the range may be abbreviated to n1. The original contents of the range follows immediately after a c indication. When the contents of two files are identical, the contents of the lower-numbered file is suppressed. Under the -e option, diff3 publishes a script for the editor ed that will incorporate into file1 all changes between file2 and file3, i.e. the changes that normally would be flagged ==== and ====3. Option -x (-3) produces a script to incorporate only changes flagged ==== (====3). The following command will apply the resulting script to `file1'. (cat script; echo '1,$p') | ed - file1 The -E and -X are similar to -e and -x, respectively, but treat overlapping changes (i.e., changes that would be flagged with ==== in the normal listing) differently. The overlapping lines from both files will be inserted by the edit script, bracketed by "<<<<<<" and ">>>>>>" lines. For example, suppose lines 7-8 are changed in both file1 and file2. Applying the edit script generated by the command "diff3 -E file1 file2 file3" to file1 results in the file: lines 1-6 of file1 <<<<<<< file1 lines 7-8 of file1 ======= lines 7-8 of file3 >>>>>>> file3 rest of file1 The -E option is used by RCS merge(1) to insure that overlapping changes in the merged files are preserved and brought to someone's atten- tion. FILES
/tmp/d3????? /usr/libexec/diff3 SEE ALSO
diff(1) BUGS
Text lines that consist of a single `.' will defeat -e. 7th Edition October 21, 1996 DIFF3(1)
All times are GMT -4. The time now is 09:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy