-2

I'd like to add a specific line to a file in a specific line number (second) without over-writing the data which is already there.

I've tried this:

sed -i '2i - jstat' FILE

But this just over-writes the second line with "- jstat".

Instead, I want to add a new second line and push the next line to be number 3. Let's say the file looks like that:

[root@puppet roles]# head -5 !$
head -5 buncher.yaml
classes:
  - workspace
  - fstab
  - role_specific

I'd like to add a new module on the second line and I want the "workspace" module to become the third line.

More than that, I'd like the new line to start with a tab size of 2 chars and then "- jstat" as in: "TabTab - jstat", how can it be done?

Itai Ganot
  • 5,873
  • 18
  • 56
  • 99

1 Answers1

0

Well to insert a line after before line number x, you can use the below sed command:

sed 'x i\LINE_TO_ADD' filename > temp_file
mv temp_file filename

Here is the example for adding <TAB><TAB>Hello How are you! to the tempfile whose content are:

123
234
345

To add before line number 2 I wrote the following sed command:

sed '2 i\\t\tHello How are you!' tempfile > temp_file
mv temp_file tempfile

Final content of tempfile are:

123
<TAB><TAB>Hello How are you!
234
345

For more detail, try referring this Sed tutorial

Rakholiya Jenish
  • 3,165
  • 18
  • 28