Adding lines at end of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding lines at end of a line
# 1  
Old 05-15-2010
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.

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

I want this as my output:
Code:
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:
Code:
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!

Last edited by Scott; 05-15-2010 at 10:32 PM.. Reason: code tags...
# 2  
Old 05-15-2010
How do you decide how many lines to add
this keeps adding one more line -- 2, 3, 4 ,5 ....
Code:
#$!/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

# 3  
Old 05-15-2010
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.
"
Code:
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:

Code:
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!

Last edited by Scott; 05-15-2010 at 10:33 PM.. Reason: Code tags, please...
# 4  
Old 05-15-2010
That is POSIX shell standard syntax i=$(( <- no spaces
If your shell does not like it use the ` ` (backtic) syntax
# 5  
Old 05-15-2010
I copied and pasted your script to no avail. Did you get it to work on your end?
# 6  
Old 05-15-2010
what shell/OS are you on?
# 7  
Old 05-15-2010
There's a typo in the shebang line in Jim's script ... but in my ksh it still works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

2. Shell Programming and Scripting

awk Adding a Period at the end of a line

I started venturing in learning the art of using AWK/GAWK and wanted to simply added a period from line #11 to line #28 or to the end of the file if there is data. So for example: 11 Centos.NM 12 dojo1 13 redhat.5.5.32Bit 14 redhat.6.2.64Bit... (5 Replies)
Discussion started by: metallica1973
5 Replies

3. Shell Programming and Scripting

Adding semicolon at the end of each line

Hi, I have a script which I need to change. I want to add a semicolon at the end of each line where the line starts with "grant" for e.g. create table(.... ); grant select on TABL1 to USER1 grant select on TABL1 to USER2should become create table(.... ); grant select on TABL1 to... (3 Replies)
Discussion started by: pparthiv
3 Replies

4. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

5. Shell Programming and Scripting

Script adding ^M to end of line

I trying to make a simple script to get info from remote servers my problem is the output of this line- SERVER_NAME=`ssh -t $USER@$REMOTESERVER 'hostname'`the output is linux1^M I would like to remove the ^M where is my error? Many Thanks -Steve (1 Reply)
Discussion started by: shoodlum
1 Replies

6. UNIX for Dummies Questions & Answers

Adding comma at the end of every line

Hi all, I have this sample file (actual file is larger) and i need to add comma at the end of every line. 1234 4335 232345 1212 3535 Output 1234, 4335, 232345, 1212, 3535, TIA - jak (2 Replies)
Discussion started by: jakSun8
2 Replies

7. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

8. Shell Programming and Scripting

adding text to the end of lines containing a word

I'm new to shell scripting, and need to add a series of commands to the ends of certain lines of text that contain a keyword. Any easy way to do this? Thanks (2 Replies)
Discussion started by: ironwall
2 Replies

9. Shell Programming and Scripting

adding semicolumn at end of line

Hi , I need to add semicolumn at the end of each line in a file. can any one help me in this? Thanks in advance (2 Replies)
Discussion started by: scorpio
2 Replies

10. Shell Programming and Scripting

Adding new line at the end of file

Hi I have few files. For some files the cursor is at the end of last line. For other files, cursor is at the new line at the end. I want to bring the cursor down to next line for the files that are having cursor at the end of last line In otherwords, I want to introduce a blank line at the... (5 Replies)
Discussion started by: somesh_p
5 Replies
Login or Register to Ask a Question