LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2009, 02:27 AM   #1
kariagekun
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Rep: Reputation: 0
compare two files and print the same text


Hi,

I want to compare two files and print the same text with awk programming, for example:

File1.txt
----------

foo
bar
leopard


File2.txt
---------

barbara dord list
foo exp dwn
don blank balnk
bar lofg been
leopard snow spring


And desired output "file3.txt"

File3.txt
----------
foo exp dwn
bar lofg been
leopard snow spring

Thanks and appreciate your help
 
Old 10-12-2009, 02:33 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by kariagekun View Post
Hi,

I want to compare two files and print the same text with awk programming, for example:

File1.txt
----------

foo
bar
leopard


File2.txt
---------

barbara dord list
foo exp dwn
don blank balnk
bar lofg been
leopard snow spring


And desired output "file3.txt"

File3.txt
----------
foo exp dwn
bar lofg been
leopard snow spring

Thanks and appreciate your help
Explain, in simple declarative sentences, what you want the program to do. If expressing yourself in clear prose is beyond your abilities, then writing a computer program is certainly out of the question.

In particular, tell us what "compare two files and print the same text" means, exactly and unambiguously.
 
Old 10-12-2009, 02:46 AM   #3
kariagekun
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Hi thanks for your reply,

I just want to print all text in file2.txt that match with text from file1.txt.

so, "foo, bar, leopard" are printed with all attribute.
Thanks
 
Old 10-12-2009, 02:46 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Looks like a homework assignment to me.

Try reading the course material that you have recently covered.
 
Old 10-14-2009, 07:24 AM   #5
Mohammad.Khaleel
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Hello
this is the solution

Quote:
#!/usr/bin/perl
open a, "<file1";
open b, "<file2";
local $/;
my @a = split /\n/, <a>;
my @b = split /\n/, <b>;
my %b = map { $_ => 1 } @b; # Make hash of B
my @res = grep { defined $b{$_} } @a;
print join "\n", @res;
print "\n";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash- how to compare only certain lines of text files daberkow Linux - Newbie 2 06-01-2009 04:48 PM
Script to compare numbers inside two text files bugg_deccan Programming 3 10-17-2007 09:53 PM
compare two files in C and print the line when they do not match calorie712 Programming 7 04-13-2006 01:56 AM
HP PS7150: text doesn't print in OOO and Abiword!, but I can print test page & w MzFF gabba Mandriva 6 10-31-2005 11:10 AM
how to compare 2 text files by using php code antony_csf Programming 3 10-14-2004 05:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration