0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Works a charm ... now to figure out how to get my end goal working :)
Ooops! I didn't see that bit. What are all the x's in the output - spaces or zeroes?
Not a problem. They're spaces :)
Thanks! You're awesome! :) just need it to start new line every 15 columns printed. I've edited my original question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.