Adding lines at end of a line

This is what I want to do. I want to write a script that reads each line (of the highlighted file below) and add a specific number of blank lines (sometime 2, 3 or 5 lines) at the end of each line while copying that line. For example, here is the input.

The sky is blue.
I like to eat.
I like nice cars.
staying healthy is fun.

I want this as my output:

The sky is blue.
The sky is blue.
The sky is blue.
I like to eat.
I like to eat.
I like to eat.
I like to eat.
I like nice cars.
staying healthy is fun.
staying healthy is fun.
staying healthy is fun.
staying healthy is fun.
staying healthy is fun.

I already tried this script:

while read line
do 
echo $line "\n \n"
done< filename

My output gave me a couple of blank lines but do not know how to add a copy of each line to those blank lines.

I hope someone can help.

Thank you!

How do you decide how many lines to add
this keeps adding one more line -- 2, 3, 4 ,5 ....

#$!/bin/ksh
cnt=2
while read line
do
   i=cnt
   while [ $i -gt 0 ]
   do
         echo "$line"
         i=$(( $i - 1 ))
   done
   cnt=$(( $cnt + 1 ))
done < inputfile

Actually, in this case, the number of lines will be arbitrary. I think I might have to number the rows first and tell the script the number of rows I need to add to a specific line. For example, I may want to add two lines to row 3 but 6 lines to row 4 and 8 lines to row 1. You get the idea.

By the way, I keep getting this error message when I run your script.
"

syntax error at line 9: `i=$' unexpected

"

---------- Post updated at 08:29 PM ---------- Previous update was at 07:22 PM ----------

Jim, I got closer by using this script:

nl testfile > testfile1
while read line
do
    echo $line "\n $line \n $line" >> sw1_LineInsert
done <testfile1

This script copies each line twice. This is not I really want. I need to figure out now how to tell the script the number of times I want to copy a specific row.

Let me know if you have any suggestions.

Thanks!

That is POSIX shell standard syntax i=$(( <- no spaces
If your shell does not like it use the ` ` (backtic) syntax

I copied and pasted your script to no avail. Did you get it to work on your end?

what shell/OS are you on?

There's a typo in the shebang line in Jim's script ... but in my ksh it still works.

ah...I was really skipping the scripted version myself... It worked so well, I got confused.

$ echo "The sky is blue.^JI like to eat.^JI like nice cars.^Jstaying healthy is fun." |while read line
> do
>   i=2
>   while [ $i -gt 0 ]
>   do
>     echo "$line"
>     i=$(( $i - 1 ))
>   done
>   i=$(( $i + 1 ))
> done

I am probably using ksh, How can I double-check for sure?

env |grep sh

But, as methyl has pointed out, the $ following the # in the first line of the script is your problem...most likely.

Ok it works but not the way I want it. I do not want an incremental count. I want instead line 1 to be copied 5 times, line 2 to be copied twice, line 3 to be copied 4 times, and line 4 to be copied 7 times. How can I achieve that?

---------- Post updated at 10:14 PM ---------- Previous update was at 10:10 PM ----------

I am thinking that maybe I need to specify the row number and tell the script to make "x" number of copies for that specific row. How should I script it?

You'd really want to re-wrap it all into its own loop to achieve this:

my_list='5 2 4 7' 
typeset -i h=1 
for n in $my_list 
do 
   sed -n ${h}p Edit4 |while read line 
   do 
      i=${n} 
      while [ $i -gt 0 ] 
      do 
         echo "$line" 
         i=$(( $i - 1 )) 
      done 
      i=$(( $i + 1 )) 
   done 
   h=$(( $h + 1 )) 
done 

When I use this script, I get the following message:

Can't open Edit4
Can't open Edit4
Can't open Edit4
Can't open Edit4

sorry, Edit4 would be replaced with your input filename.

Great!! This is exactly what I wanted to accomplish. You save me a lot of time. Many thanks!

---------- Post updated at 11:29 PM ---------- Previous update was at 11:25 PM ----------

One more thing; let's say I have a data on a spreadsheet with 1223 rows. How do I tell the script to only copy i.e. row 885 four times and row 912 five times. I am just curious to see how you would write the code.

you might slap an extra column with the frequency into the spreadsheet before you dumped it to a text file...and then tweak the looping to parse the $line to adjust your sed -n loop's $i value accordingly...

Try this bash script

# cat -n /temp/script.sh
     1  #!/bin/bash
     2
     3  if [ ${#} -lt 2 ] || [ ! -r $1 ];then
     4          echo "Usage: $0 input.file row:count row:count ....etc";
     5          exit 1;
     6  fi
     7  file=$1 && shift
     8  while (($#)); do
     9          array[${1%:*}]=${1#*:}
    10          shift
    11  done
    12  cnt1=0
    13  while read line;do
    14          let cnt1++
    15          if [ ! -z ${array[${cnt1}]} ];then
    16                  cnt2=${array[${cnt1}]}
    17                  while [ $cnt2 -gt 0 ];do
    18                          echo $line
    19                          let cnt2--
    20                  done
    21          fi
    22  done < $file

# /temp/script.sh /temp/file.in 2:3 4:5
line 2
line 2
line 2
line 4
line 4
line 4
line 4
line 4

I get this message when I run your script with the rows numbered:
./filename: 1: not found
./filename: 2: not found
./filename: syntax error at line 5: `then' unexpected

Without the rows numbered, I have this message:
Usage: ./filename input file row:count row:count ....etc....et
Usage:: not found

Post your script to see where the problem is :rolleyes:

I manipulated the script to get me exactly what I want. But I have an issue with how the output looks. I am getting something smilar to this:
a mar bill
a mar bill
a mar bill
three tree cat
three tree cat
three tree cat
ell ell elll
ell ell elll
ell ell elll

Remember I am dealing with a huge database. When I open with excel, I have to go through each line to delete the empty cells. How can I write a program that will read the file and remove the empty space at the beginning of the 2nd and third line?

If someone could help with that, that would be fantastic.