LinuxQuestions.org
Review your favorite Linux distribution.
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 07-21-2011, 12:49 PM   #1
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Rep: Reputation: 40
compare 2 text files and change lines


hello,
quick question:
in the shell. how do i compare 2 text files and change matching lines in one of the files ?

example:
cat file_1.txt
line_1
line_10
<eof>

cat file_2.txt
line_1
line_2
...
line_10
...
line_1000
<eof>
the result is file_3.txt
cat file3_txt:
# line_1
line_2
...
#line_10
...
line_1000
<eof>

Thank you.
Andrew
 
Old 07-21-2011, 01:05 PM   #2
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
man diff
 
Old 07-21-2011, 01:13 PM   #3
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by rch View Post
man diff
isn't it give you only those that are _not_ matching ?
 
Old 07-21-2011, 01:19 PM   #4
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Original Poster
Rep: Reputation: 40
ok, an "ugly" approach:
take the file_1 and make it into file with sed's expressions. then feed to sed ....
 
Old 07-21-2011, 07:01 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Actually, there's a whole load of diff type cmds http://linux.die.net/man/1/diff (see bottom of page).
I think you can get almost anything ie common and/or differences.
See also http://linux.die.net/man/1/comm
 
Old 07-22-2011, 12:11 PM   #6
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by chrism01 View Post
Actually, there's a whole load of diff type cmds http://linux.die.net/man/1/diff (see bottom of page).
I think you can get almost anything ie common and/or differences.
See also http://linux.die.net/man/1/comm
Chrism,
that's what i was thinking initially - get the common lines, feed them to sed and call it a day.

But then i started thinking - isn't it too much of file reading for such a simple operation - read 2 files to get common. Store in a third file. Read two files again to find matching lines and store the output to the final one.
There got to be a utility that does just this one step. So for now i settle with "ugly" approach, but i'm still left wondering...
 
Old 07-26-2011, 01:20 PM   #7
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Original Poster
Rep: Reputation: 40
aha! and i just came across the (almost complete) solution http://www.linuxquestions.org/questi...2/#post4425011
 
  


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
How to compare/diff a range of lines from two text files jedibrand Linux - Software 1 03-26-2010 01:54 PM
compare two files and print the same text kariagekun Linux - Newbie 4 10-14-2009 07:24 AM
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
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:04 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