0

This post indicates how to insert text in specific lines of a file.

Instead of inserting text I would like to inseart a repetition of a number. For example this series: 2 2 2 2 2 2 2 ... (100 times)

  • It might be better to edit your question to make it independent on other posts. Do you want to insert a line formed of 2 2 2 ...? Where do you want to insert it? – Satō Katsura Feb 21 '17 at 14:20

1 Answers1

1

Generate the text you'd like inserted:

$ perl -e 'print "2 " x 99, "2\n"' >insert

Insert it into the file (on line 4 in this example):

$ cat file
The
Dog
Is
Here

$ sed '3r insert' file >file.tmp && mv file.tmp file

$ cat file
The
Dog
Is
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Here

The sed editing command r ("read") will append the contents of a given file on the next line.

Kusalananda
  • 333,661
  • 1
    can be combined as single perl command... perl -i -pe 'print "2 " x 99, "2\n" if $. == 4' file.. or if extra space at end is not an issue, perl -i -lpe 'print "2 " x 100 if $. == 4' file – Sundeep Feb 21 '17 at 15:20