The problem is this line:
for number2 in {1..${loops}}
This is a question of precedence. As explained in man bash
:
The order of expansions is: brace expansion; tilde expansion,
parameter and variable expansion, arithmetic expansion, and command
substitution (done in a left-to-right fashion); word splitting;
and pathname expansion.
So, brace expansion happens before variable expansion. Therefore, when the braces are expanded, $loops
has not been evaluated to 5
. This means that {1..$loops}
(you don't need the second {}
) expands to the string {1..5}
and not the sequence of numbers from 1 to 5. The same thing happens if you use a string instead of a variable:
$ echo {1..5} ## works: prints a sequence
1 2 3 4 5
$ echo {1..foo} ## fails: prints a string with no expansion
{1..foo}
As a workaround, you can use seq
:
for number2 in $(seq 1 "$loops"); do ...
Or, even better, the right loop for the job:
for((number2=0;number2<=$loops; number2++)); do ...
Note: As a general rule, you should also always quote your variables.
If you want to extract the non parenthesized values of the SEM
attribute then you can do so using grep
in PCRE mode:
$ grep -Po '(?<=\[SEM=)[^()]*?(?=\])' file
50
'times'
4
or perl
itself
$ perl -lne 'print $1 if /(?<=\[SEM=)([^()]*?)(?=\])/' file
50
'times'
4
Both approaches use regular expression lookarounds.
Best Answer
Bash doesn't support that kind of syntax directly. You could use 'eval', however with modern versions of 'bash' the cleanest way imho is via an array
(note that bash arrays are zero-indexed); or to list all elements you can use