Gentoo Archives: gentoo-user

From: Etaoin Shrdlu <shrdlu@×××××××××××××.org>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: sed on the commandline
Date: Sat, 12 Feb 2011 13:27:19
Message-Id: 201102121325.p1CDPdYo023292@dcnode-01.unlimitedmail.net
In Reply to: Re: [gentoo-user] OT: sed on the commandline by meino.cramer@gmx.de
1 On Sat, 12 Feb 2011 14:11:20 +0100
2 meino.cramer@×××.de wrote:
3
4 > Alan McKinnon <alan.mckinnon@×××××.com> [11-02-12 13:44]:
5 > > Apparently, though unproven, at 13:25 on Saturday 12 February 2011,
6 > > meino.cramer@×××.de did opine thusly:
7 > >
8 > > > Hi,
9 > > >
10 > > > I am trying to instruct sed to insert a line of text before
11 > > > a matched line. The whole command should fit into one
12 > > > physical (command) line.
13 > > >
14 > > > Is it possible? And how is it possible?
15 > > >
16 > > > Thank you very much for any hint in advance!
17 > > > Best regards,
18 > > > mcc
19 > >
20 > >
21 > > There's nothing special about a line, it's just a bunch of characters
22 > > that end with a newline (itself just a character).
23 > >
24 > > But you can't insert stuff at arbitrary points, you can only replace
25 > > stuff with other stuff. You can replace the start of line marker (^),
26 > > so do this:
27 > >
28 > > $ cat sed.txt
29 > > 1
30 > > 2
31 > > $ cat sed.txt | sed -e 's/^/a\n/g'
32 > > a
33 > > 1
34 > > a
35 > > 2
36 > >
37 > > I replaced "start of line" with "a and a newline". Modify the regex to
38 > > suit your needs. This gets awkward though, as you can search with a
39 > > regex but only replace a literal. If you need to insert some line
40 > > before any line containing say a "z" for example, then that is way
41 > > beyond sed's capabilities and you are into awk|perl territory.
42 > >
43 > > You didn't clearly state what you are trying to do with examples, so
44 > > the above vague wishy-washy goop is the best I can do for you.
45 > >
46 > >
47 > > --
48 > > alan dot mckinnon at gmail dot com
49 > >
50 >
51 > Hi,
52 >
53 > I update my MakeHuman svn source and the Blender svn source on a daily
54 > basis. Currently the Blender folks did a change in the registration
55 > code for Blender scripts. The MakeHuman folks provide a script, which
56 > is needed to load the putput of MakeHuman into Blender. This script
57 > isn't "new registration ready".
58 >
59 > I have to do the following the changes to the Makehuman script (a
60 > handfull):
61 >
62 > change this: =======================================
63 > def registration()
64 > <script specific stuff
65 >
66 >
67 > def unregistration()
68 > <script specific stuff
69 >
70 > into this: =========================================
71 > def registration()
72 > bpy.utils.register_module(__name__)
73 > <script specific stuff
74 >
75 >
76 > def unregistration()
77 > bpy.utils.unregister_module(__name__)
78 > <script specific stuff
79 >
80
81 So it looks like you have to add a line *after* a match, not before as you
82 originally said. Try this then:
83
84 sed '/matchingline/s/$/\ninsertedline/'
85
86 which in your case will likely be something like
87
88 sed '/def registration()/s/$/\nbpy.utils.register_module(__name__)/'
89
90 you can do both insertions in a single sed script, eg
91
92 sed '/def registration()/s/$/\nbpy.utils.register_module(__name__)/
93 /def unregistration()/s/$/\nbpy.utils.unregister_module(__name__)/'

Replies

Subject Author
Re: [gentoo-user] OT: sed on the commandline meino.cramer@×××.de