Unix Shell - ISO one-line patching command

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2004 > ISO one-line patching command





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author ISO one-line patching command
kj

2004-04-25, 10:34 am



I am writing a bash script that performs an elaborate software
installation. One step in this installation involves changing a
single line in a text file in the distributed source code, from

use Test::More tests => 11;

to

use Test::More tests => 9;

I could have my installation script generate a patch file and apply
it, but it seems to me that there ought to be simpler ways for my
script to carry out this simple patching operation.

One possibility would be:

perl -i -pe 's/^(use Test::More tests => ) 11;$/$1 9/' file_to_patch

....but this requires perl, which may not be available in all
environments.

Any better (simpler and/or more portable) alternatives?

Thanks!

kj
--
NOTE: In my address everything before the period is backwards.
Bob Paulsen

2004-04-25, 11:33 am

On Sun, 25 Apr 2004 14:36:11 +0000, kj wrote:

>
>
> I am writing a bash script that performs an elaborate software
> installation. One step in this installation involves changing a single
> line in a text file in the distributed source code, from
>
> use Test::More tests => 11;
>
> to
>
> use Test::More tests => 9;
>
> I could have my installation script generate a patch file and apply it,
> but it seems to me that there ought to be simpler ways for my script to
> carry out this simple patching operation.
>
> One possibility would be:
>
> PERL -i -pe 's/^(use Test::More tests => ) 11;$/$1 9/' file_to_patch
>
> ...but this requires perl, which may not be available in all environments.
>
> Any better (simpler and/or more portable) alternatives?
>


How about using sed:

$ mv file file-orig
$ sed '/use Test::More tests => 11;/s/11/9/' < file-orig > file

Here is a bash-only solution (but I'm not sure if it is truly robust
on a variety of file formats):

============ fixit.sh =================================
#!/bin/bash
IFS=
while read line ; do
[ "$line" == "use Test::More tests => 11;" ] &&
echo "use Test::More tests => 9;" ||
echo "$line"
done;
========================================
================

Use it like this:

$ mv file file-orig
$ ./fixit.sh < file-orig > file



Charles Demas

2004-04-25, 12:34 pm

In article <c6gicr$r6e$1@reader2.panix.com>, kj <socyl@987jk.com> wrote:
>
>
>I am writing a bash script that performs an elaborate software
>installation. One step in this installation involves changing a
>single line in a text file in the distributed source code, from
>
> use Test::More tests => 11;
>
>to
>
> use Test::More tests => 9;
>
>I could have my installation script generate a patch file and apply
>it, but it seems to me that there ought to be simpler ways for my
>script to carry out this simple patching operation.
>
>One possibility would be:
>
> PERL -i -pe 's/^(use Test::More tests => ) 11;$/$1 9/' file_to_patch
>
>...but this requires perl, which may not be available in all
>environments.
>
>Any better (simpler and/or more portable) alternatives?


how about using sed and mv like this (untested)?

sed '/use Test::More tests => 11;/s/11/9/' infile > outfile
mv outfile infile


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
Juha Laiho

2004-04-25, 2:34 pm

kj <socyl@987jk.com> said:
>I am writing a bash script that performs an elaborate software
>installation. One step in this installation involves changing a
>single line in a text file in the distributed source code, from
>
> use Test::More tests => 11;
>
>to
>
> use Test::More tests => 9;


How about using "diff -e" to create the appropriate 'ed' commands to
make this change, and upon installation run the generated commands
through 'ed'.

Not much different from 'patch', but doesn't require the patch program
to be available, and has a way to programmatically generate the
differences.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
kj

2004-04-26, 2:34 pm


Many thanks for the replies.

In <c6guuc$5p4$3@ichaos.ichaos-int> Juha Laiho <Juha.Laiho@iki.fi> writes:

>kj <socyl@987jk.com> said:
[vbcol=seagreen]
>How about using "diff -e" to create the appropriate 'ed' commands to
>make this change, and upon installation run the generated commands
>through 'ed'.


That's an intriguing idea. The output of "diff -e" is simply:

12c
use Test::More tests => 9;
..

Would I have to put these commands in a separate file for ed to
run them, or could I feed them to ed directly?

Thanks!

kj

--
NOTE: In my address everything before the period is backwards.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com