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.
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.
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
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?
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...
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
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.