LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 03-09-2009, 12:21 AM   #1
Kikazaru
LQ Newbie
 
Registered: Nov 2003
Location: Chiba, Japan
Distribution: Ubuntu 10.10 AMD64
Posts: 17

Rep: Reputation: 0
Question diff / patch ignoring changes to particular lines


Does anyone know how to patch a file, while not changing lines matching a pattern?

I know there is an -I argument to diff, but this only works if every line in the hunk matches the regexp you supply. If you could make every hunk be only 1 line long it would be okay... but this doesn't seem to be an option.

Here's an example to clarify what I want to do:

file1 (5 lines):

line1
line2
line3
line4 retain
line5

file2 (6 lines):

line1
new line2
line3
new line 4
line5
line6

result (6 lines):

line1
new line2
line3
line4 retain
line5
line6
 
Old 03-09-2009, 10:47 AM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Independance, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 160Reputation: 160
Quote:
Originally Posted by Kikazaru View Post
Does anyone know how to patch a file, while not changing lines matching a pattern?
...<SNIP>...
line5
line6
In the example you're giving it would be easier to just remove that bit from the patch.
 
Old 03-09-2009, 10:57 AM   #3
Kikazaru
LQ Newbie
 
Registered: Nov 2003
Location: Chiba, Japan
Distribution: Ubuntu 10.10 AMD64
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks for your tip. I tried this (though I suspect not exhaustively), but it seems a bit tricky to adjust the patch. I mean, if I just do a "grep -v retain" on the output that diff generates then it's not going to match the to-be-replaced hunks anymore. I realized that I could do a "diff -c" and then filter for lines beginning with ! and containing my string "retain" using awk to keep them but just remove the leading !. But then there's gonna be a corresponding line in the new hunk which is marked with a !, and again, it's not going to match unless I manually count and remove the corresponding ! in the new hunk. This could also yield hunks which don't have any changes (I suppose that might not be a problem), but this is really a serious pain to implement.

Is there a way to generate a patch file that can be filtered easily?
 
  


Reply

Tags
diff, patch


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help ! Fedora Core 9 patch Dazuko patch-dazuko-linux-2.6.26.diff.gz R03L Fedora 3 10-22-2008 09:11 AM
how to use diff to only print lines fancylad Linux - General 7 07-08-2008 04:34 PM
Using diff and erasing the lines that match ElectroLinux Linux - Newbie 4 09-20-2006 03:34 AM
patch -p1 x.diff but no patch command!? pyenos Linux - Hardware 1 03-04-2005 03:10 PM
diff-patch mtb Linux - Software 1 03-23-2003 01:49 PM


All times are GMT -5. The time now is 10:17 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration