Kumaresan Kumaresan - 1 month ago 7
Linux Question

Regex for checking binary transferred ASCII space In Bash

I'm copying a csv file from windows to linux in binary mode.
And in bash I want to validate the content, with specific character set like alphanumeric and some special chars.

re='^[-a-zA-Z0-9. ,\/()&_]*$'
if [[ ! $line =~ $re ]]
then
echo "Found undefined character in :$line"
break
fi


on sending a line ...

ABCD EFG, ABCD EFG, ABCD EFG


regx not recognizing the space. since it was copied from windows in binary mode.
moment when i give them linux space. regx recognizing it as a space.

how to handle this?
i can't change the transfer mode to ascii.

Answer

To allow for optional \r (DOS line end) at the end of your input you can use this regex:

re=$'^[-a-zA-Z0-9. ,/()&_]+\r?$'

Now test it:

line=$'ABCD EFG, ABCD EFG, ABCD EFG\r'
[[ $line =~ $re ]] && echo "valid" || echo "invalid"

Output:

valid