[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: q// / qq// syntax or built-ins please ?
From: |
Vladimir Marek |
Subject: |
Re: q// / qq// syntax or built-ins please ? |
Date: |
Fri, 24 Mar 2017 09:29:00 +0100 |
User-agent: |
Mutt/1.5.22.1-rc1 (2013-10-16) |
While quoting can be difficult at time, one can make his life simpler
DELIM=\'
CHAR="[^$DELIM]"
REGEX="$CHAR*$DELIM($CHAR+)$DELIM$CHAR*"
$ echo "
Error: Cannot find file '/missing/file_1'.
Error: Cannot find file '/missing/file_2'.
" | while read line; do
if [[ $line =~ $REGEX ]]; then
echo "${BASH_REMATCH[1]}"
fi
done
/missing/file_1
/missing/file_2
Care has to be taken though, CHAR can't be for example
CHAR=ab
WORD="$CHAR*"
as that becomes 'ab*' which is not '(ab)*'
Personally, I would probably use perl.
--
Vlad
On Thu, Mar 23, 2017 at 08:40:35PM +0000, Jason Vas Dias wrote:
> Please , in some future versions of bash, could it provide
> support / help for avoiding "quoting hell", in such situations
> as :
>
> $ echo "
>
> Error: Cannot find file '/missing/file_1'.
>
> Error: Cannot find file '/missing/file_2'.
>
> " | while read line; do
> cmd='if [[ '$'"'"$line"$'"'' =~
> ^[^'"\\'"']*['"\\'"']([^'"\\'"']+)['"\\'"'][^'"\\'"']*$ ]]; then echo
> ${BASH_REMATCH[1]}; fi;';
> echo "$cmd" | bash -;
> done
>
> See what I have to do to match lines containing a non-empty
> single-quoted string ?
> ie. I just want to cut-and-paste such lines from the output of some
> application, and
> weed out the empty lines and print the single-quoted string in lines
> containing them (only!), with a simple bash command. If you replace
> "echo $cmd | bash -" with
> 'eval "$cmd"' , it does not work, because the double-quotes which I
> had painstakingly inserted with '$'"'' get removed somehow by eval -
> why is this?
> ie, only if "$line" is empty, does bash evaluate the text:
> 'if [[ "" = ... ]]; then ...'
> else, for the lines I want to match, it would evaluate eg. :
> + eval 'if [[ Error: Cannot find file '\''/missing_file_1'\''.\" =~
> ^[^\\\'\'']*[\\\'\'']([\\\'\'']+)[\\\'\''][^\\\'\'']*$ ]]; then echo
> ${BASH_REMATCH[1]}; fi;'
> + set +x
> ( nothing printed - the single quotes are stripped)
> + eval 'if [[ \"\" =~
> ^[^\\\'\'']*[\\\'\'']([\\\'\'']+)[\\\'\''][^\\\'\'']*$ ]]; then echo
> ${BASH_REMATCH[1]}; fi;'
> ++ [[ "" =~ ^[^\']*[\']([\']+)[\'][^\']*$ ]]
> + set +x
> +
>
> I think bash needs some kind of "q/.../'" and 'qq/../' syntax / built-ins, or
> whatever syntax its author likes, like PERL has, whereby the single quote
> ("'") or double quote ('"') respectively are totally ignored within
> '/.../' parameter
> strings, which should use a different quoting character than '"' or
> "'" to delineate
> them.
>
> If the author won't develop these, I will & will send a patch.
>
> Regards,
> Jason
>