I am making a bash script. I first grab a line with grep from a realtime updating output (top, airodump-ng) then I am trying to parse that line and extract certain substrings. My problem is the ${VAR:start:length} syntax does not work and is unpredictable. I will show some examples,
My original unparsed string is this, I am trying to extract the two Mac addresses (which I made up)
VAR="12:34:56:78:91:23 23:45:67:89:12:34 -37 24e-24e 0 23"
In the script, var is defined by VAR="airodump-ng | grep ...
but for this post it is a string literal.
So maybe there is some random whitespace characters messing with it from grep. Anyways, when I try to take substrings of VAR this is what happens,
SUB=${VAR:1:25}
12:34:56:78:91:23 23:45:
Good
SUB${VAR:1:24}
12:34:56:78:91:23 23:45
Good
SUB${VAR:1:23}
12:34:56:78:91:
Bad
You can see now out of nowhere the substring is much shorter than expected, going from 24->23 does not make it one character shorter but 9 shorter. This patter continues, for example, 12, 13, 14 work but 15 is way to short again. The same thing happens with the cut command echo $VAR | cut -c1-25
with the same indexes.
So how can I fix this and why is it happening. Or are there any better solutions to extract these substrings?
.