Gentoo Archives: gentoo-user

From: Alan McKinnon <alan.mckinnon@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: sed on the commandline
Date: Sat, 12 Feb 2011 12:37:29
Message-Id: 201102121436.24218.alan.mckinnon@gmail.com
In Reply to: [gentoo-user] OT: sed on the commandline by meino.cramer@gmx.de
1 Apparently, though unproven, at 13:25 on Saturday 12 February 2011,
2 meino.cramer@×××.de did opine thusly:
3
4 > Hi,
5 >
6 > I am trying to instruct sed to insert a line of text before
7 > a matched line. The whole command should fit into one
8 > physical (command) line.
9 >
10 > Is it possible? And how is it possible?
11 >
12 > Thank you very much for any hint in advance!
13 > Best regards,
14 > mcc
15
16
17 There's nothing special about a line, it's just a bunch of characters that end
18 with a newline (itself just a character).
19
20 But you can't insert stuff at arbitrary points, you can only replace stuff
21 with other stuff. You can replace the start of line marker (^), so do this:
22
23 $ cat sed.txt
24 1
25 2
26 $ cat sed.txt | sed -e 's/^/a\n/g'
27 a
28 1
29 a
30 2
31
32 I replaced "start of line" with "a and a newline". Modify the regex to suit
33 your needs. This gets awkward though, as you can search with a regex but only
34 replace a literal. If you need to insert some line before any line containing
35 say a "z" for example, then that is way beyond sed's capabilities and you are
36 into awk|perl territory.
37
38 You didn't clearly state what you are trying to do with examples, so the above
39 vague wishy-washy goop is the best I can do for you.
40
41
42 --
43 alan dot mckinnon at gmail dot com

Replies

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