1 |
On 02/12/2011 06:25 AM, meino.cramer@×××.de wrote: |
2 |
> |
3 |
> Hi, |
4 |
> |
5 |
> I am trying to instruct sed to insert a line of text before |
6 |
> a matched line. The whole command should fit into one |
7 |
> physical (command) line. |
8 |
> |
9 |
> Is it possible? And how is it possible? |
10 |
> |
11 |
> Thank you very much for any hint in advance! |
12 |
> Best regards, |
13 |
> mcc |
14 |
> |
15 |
> |
16 |
> |
17 |
|
18 |
Try the ampersand "&" like the example below. |
19 |
|
20 |
Make a file of phone numbers. |
21 |
$ cat phone.txt |
22 |
555-1212 |
23 |
555-1234 |
24 |
555-9999 |
25 |
|
26 |
Then run the following command to prefix the number with 212 and a dash. |
27 |
$ sed 's/555/212-&/' phone.txt |
28 |
212-555-1212 |
29 |
212-555-1234 |
30 |
212-555-9999 |
31 |
|
32 |
Try moving the ampersand around the replacement string. If moved to the |
33 |
beginning it transposes the numbers. Note: The dash was moved to make |
34 |
the result look better, it has nothing to do with the command. |
35 |
$ sed 's/555/&-212/' phone.txt |
36 |
555-212-1212 |
37 |
555-212-1234 |
38 |
555-212-9999 |