FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo User

 
 
LinkBack Thread Tools
 
Old 02-12-2011, 10:25 AM
 
Default OT: sed on the commandline

Hi,

I am trying to instruct sed to insert a line of text before
a matched line. The whole command should fit into one
physical (command) line.

Is it possible? And how is it possible?

Thank you very much for any hint in advance!
Best regards,
mcc
 
Old 02-12-2011, 10:58 AM
Etaoin Shrdlu
 
Default OT: sed on the commandline

On Sat, 12 Feb 2011 12:25:20 +0100 meino.cramer@gmx.de wrote:

> Hi,
>
> I am trying to instruct sed to insert a line of text before
> a matched line. The whole command should fit into one
> physical (command) line.
>
> Is it possible? And how is it possible?

sed 's/matchingline/insertedline
&/'
 
Old 02-12-2011, 11:36 AM
Alan McKinnon
 
Default OT: sed on the commandline

Apparently, though unproven, at 13:25 on Saturday 12 February 2011,
meino.cramer@gmx.de did opine thusly:

> Hi,
>
> I am trying to instruct sed to insert a line of text before
> a matched line. The whole command should fit into one
> physical (command) line.
>
> Is it possible? And how is it possible?
>
> Thank you very much for any hint in advance!
> Best regards,
> mcc


There's nothing special about a line, it's just a bunch of characters that end
with a newline (itself just a character).

But you can't insert stuff at arbitrary points, you can only replace stuff
with other stuff. You can replace the start of line marker (^), so do this:

$ cat sed.txt
1
2
$ cat sed.txt | sed -e 's/^/a
/g'
a
1
a
2

I replaced "start of line" with "a and a newline". Modify the regex to suit
your needs. This gets awkward though, as you can search with a regex but only
replace a literal. If you need to insert some line before any line containing
say a "z" for example, then that is way beyond sed's capabilities and you are
into awk|perl territory.

You didn't clearly state what you are trying to do with examples, so the above
vague wishy-washy goop is the best I can do for you.


--
alan dot mckinnon at gmail dot com
 
Old 02-12-2011, 11:40 AM
dhk
 
Default OT: sed on the commandline

On 02/12/2011 06:25 AM, meino.cramer@gmx.de wrote:
>
> Hi,
>
> I am trying to instruct sed to insert a line of text before
> a matched line. The whole command should fit into one
> physical (command) line.
>
> Is it possible? And how is it possible?
>
> Thank you very much for any hint in advance!
> Best regards,
> mcc
>
>
>

Try the ampersand "&" like the example below.

Make a file of phone numbers.
$ cat phone.txt
555-1212
555-1234
555-9999

Then run the following command to prefix the number with 212 and a dash.
$ sed 's/555/212-&/' phone.txt
212-555-1212
212-555-1234
212-555-9999

Try moving the ampersand around the replacement string. If moved to the
beginning it transposes the numbers. Note: The dash was moved to make
the result look better, it has nothing to do with the command.
$ sed 's/555/&-212/' phone.txt
555-212-1212
555-212-1234
555-212-9999
 
Old 02-12-2011, 12:07 PM
Etaoin Shrdlu
 
Default OT: sed on the commandline

On Sat, 12 Feb 2011 14:11:20 +0100
meino.cramer@gmx.de wrote:

> Alan McKinnon <alan.mckinnon@gmail.com> [11-02-12 13:44]:
> > Apparently, though unproven, at 13:25 on Saturday 12 February 2011,
> > meino.cramer@gmx.de did opine thusly:
> >
> > > Hi,
> > >
> > > I am trying to instruct sed to insert a line of text before
> > > a matched line. The whole command should fit into one
> > > physical (command) line.
> > >
> > > Is it possible? And how is it possible?
> > >
> > > Thank you very much for any hint in advance!
> > > Best regards,
> > > mcc
> >
> >
> > There's nothing special about a line, it's just a bunch of characters
> > that end with a newline (itself just a character).
> >
> > But you can't insert stuff at arbitrary points, you can only replace
> > stuff with other stuff. You can replace the start of line marker (^),
> > so do this:
> >
> > $ cat sed.txt
> > 1
> > 2
> > $ cat sed.txt | sed -e 's/^/a
/g'
> > a
> > 1
> > a
> > 2
> >
> > I replaced "start of line" with "a and a newline". Modify the regex to
> > suit your needs. This gets awkward though, as you can search with a
> > regex but only replace a literal. If you need to insert some line
> > before any line containing say a "z" for example, then that is way
> > beyond sed's capabilities and you are into awk|perl territory.
> >
> > You didn't clearly state what you are trying to do with examples, so
> > the above vague wishy-washy goop is the best I can do for you.
> >
> >
> > --
> > alan dot mckinnon at gmail dot com
> >
>
> Hi,
>
> I update my MakeHuman svn source and the Blender svn source on a daily
> basis. Currently the Blender folks did a change in the registration
> code for Blender scripts. The MakeHuman folks provide a script, which
> is needed to load the putput of MakeHuman into Blender. This script
> isn't "new registration ready".
>
> I have to do the following the changes to the Makehuman script (a
> handfull):
>
> change this: =======================================
> def registration()
> <script specific stuff
>
>
> def unregistration()
> <script specific stuff
>
> into this: =========================================
> def registration()
> bpy.utils.register_module(__name__)
> <script specific stuff
>
>
> def unregistration()
> bpy.utils.unregister_module(__name__)
> <script specific stuff
>

So it looks like you have to add a line *after* a match, not before as you
originally said. Try this then:

sed '/matchingline/s/$/
insertedline/'

which in your case will likely be something like

sed '/def registration()/s/$/
bpy.utils.register_module(__name__)/'

you can do both insertions in a single sed script, eg

sed '/def registration()/s/$/
bpy.utils.register_module(__name__)/
/def unregistration()/s/$/
bpy.utils.unregister_module(__name__)/'
 
Old 02-12-2011, 12:11 PM
 
Default OT: sed on the commandline

Alan McKinnon <alan.mckinnon@gmail.com> [11-02-12 13:44]:
> Apparently, though unproven, at 13:25 on Saturday 12 February 2011,
> meino.cramer@gmx.de did opine thusly:
>
> > Hi,
> >
> > I am trying to instruct sed to insert a line of text before
> > a matched line. The whole command should fit into one
> > physical (command) line.
> >
> > Is it possible? And how is it possible?
> >
> > Thank you very much for any hint in advance!
> > Best regards,
> > mcc
>
>
> There's nothing special about a line, it's just a bunch of characters that end
> with a newline (itself just a character).
>
> But you can't insert stuff at arbitrary points, you can only replace stuff
> with other stuff. You can replace the start of line marker (^), so do this:
>
> $ cat sed.txt
> 1
> 2
> $ cat sed.txt | sed -e 's/^/a
/g'
> a
> 1
> a
> 2
>
> I replaced "start of line" with "a and a newline". Modify the regex to suit
> your needs. This gets awkward though, as you can search with a regex but only
> replace a literal. If you need to insert some line before any line containing
> say a "z" for example, then that is way beyond sed's capabilities and you are
> into awk|perl territory.
>
> You didn't clearly state what you are trying to do with examples, so the above
> vague wishy-washy goop is the best I can do for you.
>
>
> --
> alan dot mckinnon at gmail dot com
>

Hi,

I update my MakeHuman svn source and the Blender svn source on a daily
basis. Currently the Blender folks did a change in the registration
code for Blender scripts. The MakeHuman folks provide a script, which
is needed to load the putput of MakeHuman into Blender. This script
isn't "new registration ready".

I have to do the following the changes to the Makehuman script (a
handfull):

change this: =======================================
def registration()
<script specific stuff


def unregistration()
<script specific stuff

into this: =========================================
def registration()
bpy.utils.register_module(__name__)
<script specific stuff


def unregistration()
bpy.utils.unregister_module(__name__)
<script specific stuff


until the MakeHuman folks have time to integrate my patch into their
code.

Since I do update often I would have to edit all these files by hand
every time.

It would be much more time saveing, if a sed-oneliner from the
commandline, saved into the shell history, could do this for me....

I googled a little in beforehand and found the "a" command, but I
didn't managed to get it working for me...so...

Best regards,
mcc
 
Old 02-12-2011, 12:43 PM
 
Default OT: sed on the commandline

Etaoin Shrdlu <shrdlu@unlimitedmail.org> [11-02-12 14:36]:
> On Sat, 12 Feb 2011 14:11:20 +0100
> meino.cramer@gmx.de wrote:
>
> > Alan McKinnon <alan.mckinnon@gmail.com> [11-02-12 13:44]:
> > > Apparently, though unproven, at 13:25 on Saturday 12 February 2011,
> > > meino.cramer@gmx.de did opine thusly:
> > >
> > > > Hi,
> > > >
> > > > I am trying to instruct sed to insert a line of text before
> > > > a matched line. The whole command should fit into one
> > > > physical (command) line.
> > > >
> > > > Is it possible? And how is it possible?
> > > >
> > > > Thank you very much for any hint in advance!
> > > > Best regards,
> > > > mcc
> > >
> > >
> > > There's nothing special about a line, it's just a bunch of characters
> > > that end with a newline (itself just a character).
> > >
> > > But you can't insert stuff at arbitrary points, you can only replace
> > > stuff with other stuff. You can replace the start of line marker (^),
> > > so do this:
> > >
> > > $ cat sed.txt
> > > 1
> > > 2
> > > $ cat sed.txt | sed -e 's/^/a
/g'
> > > a
> > > 1
> > > a
> > > 2
> > >
> > > I replaced "start of line" with "a and a newline". Modify the regex to
> > > suit your needs. This gets awkward though, as you can search with a
> > > regex but only replace a literal. If you need to insert some line
> > > before any line containing say a "z" for example, then that is way
> > > beyond sed's capabilities and you are into awk|perl territory.
> > >
> > > You didn't clearly state what you are trying to do with examples, so
> > > the above vague wishy-washy goop is the best I can do for you.
> > >
> > >
> > > --
> > > alan dot mckinnon at gmail dot com
> > >
> >
> > Hi,
> >
> > I update my MakeHuman svn source and the Blender svn source on a daily
> > basis. Currently the Blender folks did a change in the registration
> > code for Blender scripts. The MakeHuman folks provide a script, which
> > is needed to load the putput of MakeHuman into Blender. This script
> > isn't "new registration ready".
> >
> > I have to do the following the changes to the Makehuman script (a
> > handfull):
> >
> > change this: =======================================
> > def registration()
> > <script specific stuff
> >
> >
> > def unregistration()
> > <script specific stuff
> >
> > into this: =========================================
> > def registration()
> > bpy.utils.register_module(__name__)
> > <script specific stuff
> >
> >
> > def unregistration()
> > bpy.utils.unregister_module(__name__)
> > <script specific stuff
> >
>
> So it looks like you have to add a line *after* a match, not before as you
> originally said. Try this then:
>
> sed '/matchingline/s/$/
insertedline/'
>
> which in your case will likely be something like
>
> sed '/def registration()/s/$/
bpy.utils.register_module(__name__)/'
>
> you can do both insertions in a single sed script, eg
>
> sed '/def registration()/s/$/
bpy.utils.register_module(__name__)/
> /def unregistration()/s/$/
bpy.utils.unregister_module(__name__)/'
>
>
Hi,

thank you for this hint!

My first posting was made at a time as I thought only one script
has this issue. For this one script I had matched another line inside
of register(). But all scripts of MakeHuman are affected...so...

Anyway...your solution works as a charme and therefor the scripts are
registered with blender again

Have a nice weekend!
Best regards,
mcc
 

Thread Tools




All times are GMT. The time now is 12:35 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org