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
# 15  
Old 05-16-2010
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.
# 16  
Old 05-16-2010
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...

Last edited by curleb; 05-16-2010 at 01:13 AM.. Reason: clarification...
# 17  
Old 05-16-2010
Quote:
Originally Posted by Ernst
I am just curious to see how you would write the code.
Try this bash script
Code:
# 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

# 18  
Old 05-16-2010
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

Last edited by Ernst; 05-16-2010 at 03:39 PM..
# 19  
Old 05-16-2010
Quote:
Originally Posted by Ernst
I get this message when I run your script with the rows numbered:
Post your script to see where the problem is Smilie
# 20  
Old 05-17-2010
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.
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