LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-28-2008, 04:52 AM   #1
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Fedora 15
Posts: 628

Rep: Reputation: 35
sed - multiline search/replace with wildcard troubles


Hello,

I'm writing a script to upgrade GPLv2 to GPLv3 across multiple files with multiple types of comments.The idea is that it should replace regardless of the file being a Makefile (each line starting with #), a C++/h file (no prefix) or a troff file (each file starting with .\").

Here's what only works if there are no prefixes of # or .\" :

Code:
/Software Foundation/{
N
s/\(.*\)\n/\1 /
s/^\((# )|(.\" )\)*Software Foundation; either version 2, or (at your option) any later \((# )|(.\" )\)version/\1Software Foundation, either version 3 of the License, or (at your \
\1option) any later version/
}
What I do is first search for Software Foundation in order to know where to start, then to join the two lines across which the pattern is located, and then try to put the option comment-prefixes (# or .\") into optional* hold-patterns...

Can anyone please help me by a clue as to where I've gone wrong?

Thanks in advance!

-y1

Last edited by Yalla-One; 12-28-2008 at 04:53 AM. Reason: more verbose
 
Old 12-28-2008, 01:33 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 299

Rep: Reputation: 115Reputation: 115
The following uses GNU 'sed' with basic regular expressions (as used by 'grep'), not extended regular expressions (as used by GNU 'sed -r' and 'egrep'). Your code seems to mix elements of both.
Code:
Your pattern
\((# )|(.\" )\)*
should be something like
\(# \|\.\\" \)\?
The literal brackets have been removed, then | . and \ escaped.
In GNU sed \? is the same as * but only matches zero or one times

\1 can be used to repeat the pattern on the left side of the s command.
There's no need to remove the \n created by the N command as it can be matched in the patterns.
The variables a,b,A,B,prefix are used here just to make the logic clearer.
Code:
a='Software Foundation; either version 2, or (at your option) any later'
b='version'
A='Software Foundation, either version 3 of the License, or (at your '
B='option) any later version'
prefix='# \|\.\\" '
sed '/Software Foundation/{
N
'"s/^\($prefix\)\?$a\n\1$b/\1$A\n\1$B/"'
}' file

# Software Foundation, either version 3 of the License, or (at your
# option) any later version
.\" Software Foundation, either version 3 of the License, or (at your
.\" option) any later version
Software Foundation, either version 3 of the License, or (at your
option) any later version
 
Old 12-28-2008, 03:04 PM   #3
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Fedora 15
Posts: 628

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by Kenhelm View Post
Code:
a='Software Foundation; either version 2, or (at your option) any later'
b='version'
A='Software Foundation, either version 3 of the License, or (at your '
B='option) any later version'
prefix='# \|\.\\" '
sed '/Software Foundation/{
N
'"s/^\($prefix\)\?$a\n\1$b/\1$A\n\1$B/"'
}' file
You make it look so simple :-)

Thanks much - that hit the spot! Much appreciated!

-y1
 
Old 12-28-2008, 03:18 PM   #4
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
Are these files that you authored? You man not be able to change the license of source that you didn't write. It takes the consent of all the contributers to do that. Even Linus can't change the license of the Linux Kernel on his own.

IIRC, there was a clause added to allow upgrading the GPL, but not all GPL2 licenses have it.
Also, the license of a project is probably included during the make or autoconf process to avoid needing to enter it into each file manually. There may be a single source for many projects.

Last edited by jschiwal; 12-28-2008 at 03:22 PM.
 
Old 12-29-2008, 12:01 PM   #5
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Fedora 15
Posts: 628

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by jschiwal View Post
Are these files that you authored? You man not be able to change the license of source that you didn't write. It takes the consent of all the contributers to do that. Even Linus can't change the license of the Linux Kernel on his own.
I am aware of this. The author of the package in question asked me (as a non-programmer) for assistanc in converting the old source files from GPLv2 to GPLv3 and has confirmed that the other stakeholders are on par with this. I'm simply troing to help with the actual text replacement before realizing that I was in a bit over my head :-)
For older programs, the gpl text is hard coded into the headers, not set by autoconf... This one was last modified a few years ago.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed and multiline delete aliyesami Programming 3 06-27-2008 02:03 PM
sed search replace tomerbd1 Linux - General 9 04-10-2008 04:31 AM
sed question for search and replace jakev383 Linux - General 8 05-05-2007 05:40 AM
Bit complicated sed search & replace sharathkv25 Programming 6 04-07-2007 01:53 PM
sed search & replace sharathkv25 Programming 2 03-07-2007 10:16 AM


All times are GMT -5. The time now is 03:49 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