Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a file containing 2 variables, start and end that I want invoked in a for loop of a bash script but I can't seem to get the result I want

outputfile2.text 

START=26 ; END=47
START=48 ; END=69
START=70 ; END=91
START=92 ; END=113
START=114 ; END=135
...

The loop:

range.bash

#!/bin/bash
rm range.text -f

while read line
do
    for (( c=$START; c<=$END; c++ ))
    do
            echo -n "$c ";
    done >> range.text
done < "outputfile2.text"

Desired output:

1 2 3 ... 19 20 21
26 27 28 ... 45 46 47
48 49 50 ... 67 68 69
70 71 72 ... 89 90 91
... etc

How can this be done?

I'm currently getting:

./range.bash: line 6: ((: c=: syntax error: operand expected (error token is "=")

So I'm assuming it's not reading outputfile2.text as I has hoped.


END GOAL: What I really want is it to print a grid that is 15 columns wide with each entry being 6 characters long, separated by a space and titled with something, but I have no clue how to code that:

line 1
xxxx1 xxxx2 xxxx3 xxxx4 xxxx5 xxxx6 xxxx7 xxxx8 xxxx9 xxx10 xxx11 xxx12 xxx13 xxx14 xxx15
xxx16 xxx17 xxx18 xxx19 xxx20 xxx21

line 2
xxx48 xxx49 xxx50 xxx51 xxx52 xxx53 xxx54 xxx55 xxx56 xxx57 xxx58 xxx59 xxx60 xxx61 xxx62
xxx63 xxx64 xxx65 xxx66 xxx67 xxx68

line 3
... etc

UPDATE (thanks to Mark Setchell):

#!/bin/bash

while IFS="=;" read a START c END e
do
   ((j++))
   echo "[ L${j} ]"
   for (( i=$START; i<=$END ; i++ ))
   do
      printf "%6s" $i
   done
   echo ; echo                          # Just a newline
done < outputfile2.text >> range.text

Produces:

[ L1 ]
    26    27    28    29    30    31    32    33    34    35    36    37    38    39    40    41    42    43    44    45    46    47

[ L2 ]
    48    49    50    51    52    53    54    55    56    57    58    59    60    61    62    63    64    65    66    67    68    69

almost there!! :)

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can do something like this:

#!/bin/bash
COLS=15
line=1
while IFS="=;" read a START c END e
do
   echo Line: $line
   col=1
   for (( i=$START; i<=$END ; i++ ))
   do
      printf "%6s" $i
      if [[ $col -eq 15 ]]; then
         col=1
         echo
      fi
      (( col++ ))
   done
   echo
   (( line++ ))
done < file

The trick is to set the input field separator (IFS) to separate variables when a = is seen or a ;.

share|improve this answer
    
Thanks! Works a charm ... now to figure out how to get my end goal working :) –  Patrick Jun 20 at 7:24
    
Ooops! I didn't see that bit. What are all the x's in the output - spaces or zeroes? –  Mark Setchell Jun 20 at 7:32
    
Not a problem. They're spaces :) –  Patrick Jun 20 at 7:35
1  
@Patrick Getting closer - have another look... –  Mark Setchell Jun 20 at 7:47
    
Thanks! You're awesome! :) just need it to start new line every 15 columns printed. I've edited my original question –  Patrick Jun 20 at 8:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.