Den 16. aug. 2016 03:28, Kun Zhang skreiv:
>     ## Regular Expression
>     line 338: path=$(echo "$matches" | perl -pe 's/\e\[?.*?[\@-~]//g')
>     Why not use sed or builtin?
>     Who wants to install all of perl to run bash code?
> 
> 
> I googled around but didn't find other code that successfully remove
> ANSI color codes for me.
> So I landed with the first solution that worked. I can look harder though.


rewriting the regexp to not be dependent on Perl's non-greedy syntax is
quite simple. the bash-only solution below is a bit roundabout since
there is no substitute function for regular expressions, so we have to
extract the actual plain strings using the =~ operator and then do text
substitutions:

    path="$matches"
    printf -v ansi_color_re '\033\\[[^@-~]*[@-~]'
    while [[ "$path" =~ $ansi_color_re ]]
    do
        path=${path/${BASH_REMATCH[0]}/}
    done

(note, I changed the [ after ESC to be mandatory, I think that is more
correct.)

alternatively, you can write it using GNU sed like this:

    path=$(echo "$matches" | sed 's/\o033\[[^@-~]*[@-~]//g'

or more portably:

    printf -v ESC '\033'
    path=$(echo "$matches" | sed s/${ESC}'\[[^@-~]*[@-~]//g'


-- 
Kjetil T. Homme
Redpill Linpro - Changing the game

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Password-Store mailing list
[email protected]
http://lists.zx2c4.com/mailman/listinfo/password-store

Reply via email to