0

text:

my1line
my2line
my3line
my4line
my5line
my6line
my7line
my8line
...

words that want to be inserted :

hello

desired output:

my1line
my2line
hello
my3line
my4line
hello
my5line
my6line
hello
my7line
my8line
....

Tucked in repeatedly every few lines until it runs out, is there a way to do it?

Braiam
  • 35,991

5 Answers5

5
awk '1; !(NR%2) {print "hello"}' file

Print every line. Print "hello" every second line.

rowboat
  • 2,791
4
awk 'NR%2==0{$0=$0"\nhello"}1' filename

output

my1line
my2line
hello
my3line
my4line
hello
my5line
my6line
hello
my7line
my8line
hello
4

With GNU sed's n-skip-m address operator:

sed '2~2a hello' textfile

Use the -i option if you want to edit the file in-place.

steeldriver
  • 81,074
3

With perl:

perl -pe '$_ .= "hello\n" unless $. % 2' input
1

With Gnu sed and its step operator. Test with :

seq 100 | sed '3~2 s/.*/hello\n&/'

Or with standard sed. Test with :

seq 100 | sed 'n;s/.*/&\nhello/;'

Of course use your file as input. So the preceding example become:

sed 'n;s/.*/&\nhello/;' datafile