I have an issue with bash and its regex match ability that I for now think is a bug in bash but could also be me having missed something obvious. I hope it is the latter.
I have made this function in a bash script to spilt an address into fields. There is some debug output that will be removed eventually:
# name number, zip
function split_address
{
local adr
adr="$4"
echo $adr
local adr_regex
adr_regex="[ ]*(.*[a-z ]) ([^,][^,]*),[ ]*([^ ]*)[ ]*"
[[ $adr =~ $adr_regex ]]
echo 1:X${BASH_REMATCH[1]}X
echo 2:X${BASH_REMATCH[1]%% }X
echo 3:Y${BASH_REMATCH[2]}Y
echo 4:Y${BASH_REMATCH[2]%% }Y
local x="${BASH_REMATCH[1]}"
echo 5:X${x%% }X
local x="${BASH_REMATCH[1]%% }"
echo 6:X${x}X
echo 7:X${x%% }X
eval "$1='${BASH_REMATCH[1]%% }'"
eval "$2='${BASH_REMATCH[2]%% }'"
eval "$3='${BASH_REMATCH[3]}'"
}
I test it like this:
split_address roadname number zip " Some string 42 dp , 1234 "
echo X${roadname}X Y${number}Y Z${zip}Z
When called, I get this output:
Some string 42 dp , 1234
1:XSome string X
2:XSome string X
3:Y42 dp Y
4:Y42 dpY
5:XSome string X
6:XSome string X
7:XSome stringX
XSome string X Y42 dpY Z1234Z
First note that 4 has the space shown in 3 removed. This is what I want to happen in 2 when working on 1. Notice that 5 does not get the space removed even though this happens on the variable x. This was an attempt to work around this issue. Then I tried assigning the space-removing operation to the variable x but that also failed (shown in 6). But removing spaces on x in step 7 worked even though the line is identical to 5 and the input apparently as well.
Is this me doing something weird or is this a bug in bash?
For reference, I am working on Ubuntu 14 LTS with bash version 4.3.11(1)-release.
I see the same behavior with Cygwin-x64 with bash version 4.1.17(9-release.
I have verified that the character to be removed in deed is a space (using od on both source and test-call).
echo "$addr"
and you'll see the difference. Let answer how many spaces afterstring
and how many you can strip by${...%% }
bash
as opposed toperl
or text processing utilities for that?echo
do substitute some trailing field separators (like a space e.g.) by one if you didn't put arguments into quotes.adr_regex="[ ]*(.*[a-z])[ ]*([0-9][^,]*),[ ]*([^ ]*)[ ]*"
or better include some separator into input string (for example;
) and use it in regex.