Advanced

shel script

Forum: LinuxTotal Replies: 1
Author Content
saneesh

Aug 28, 2006
8:09 PM EDT
using sed command how to replace a single line and insert multiple lines
cyber_rigger

Aug 28, 2006
9:18 PM EDT
You will need \n (newline) to make extra lines.

You might also want to read about 'regular expressions' to set the parameters of WHAT you want to replace.

Here are some one-line examples.

Let's say you have a file called file1 which contains:

1 2 3 4 5

Then, using subtitution,

cat file1 | sed 's/1/one/'

produces:

one 2 3 4 5

Using \n to make extra lines:

cat file1 | sed 's/1/one\ntwo/'

produces:

one two 2 3 4 5

If I wanted to send this output to file2

cat file1 | sed 's/1/one\ntwo/' > file2

A shorter way to write this is

sed 's/1/one\ntwo/' < file1 > file2

Post a reply to this thread:

A free account is needed to post. Write your comment here first: your text is kept in this browser while you sign up or log in, and is waiting for you when you come back.

Your message:
|||||
10
Markdown: **bold**, *italic*, [text](https://link), - lists, > quotes, $x^2$ for math. Formatting help

Sign up and post (it is free)  or  Log in and post