[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string escaping in bash
From: |
Alex fxmbsw7 Ratchev |
Subject: |
Re: string escaping in bash |
Date: |
Fri, 12 Mar 2021 19:58:51 +0100 |
i cannot decypher it sorry
however i think the chained stuff may work perfectly
tests may gotta show
On Fri, Mar 12, 2021 at 7:57 PM Peng Yu <pengyu.ut@gmail.com> wrote:
> I don't think the chained solution will work robustly. Here is what I
> figured out using a character by character-based approach. It should
> be correct regarding all input.
>
> But I am not sure whether its performance is the best.
>
> function tabnlunesc {
> declare __tabnlunesc__i
> declare -a __tabnlunesc__r
>
> for((__tabnlunesc__i=0;__tabnlunesc__i<${#1}-1;++__tabnlunesc__i)); do
> if [[ ${1:__tabnlunesc__i:1} = \\ ]]; then
> if [[ ${1:__tabnlunesc__i+1:1} = n ]]; then
>
> __tabnlunesc__r[__tabnlunesc__i]=$'\n'
> ((++__tabnlunesc__i))
> continue
> elif [[ ${1:__tabnlunesc__i+1:1} = t ]];
> then
>
> __tabnlunesc__r[__tabnlunesc__i]=$'\t'
> ((++__tabnlunesc__i))
> continue
> elif [[ ${1:__tabnlunesc__i+1:1} = \\ ]];
> then
>
> __tabnlunesc__r[__tabnlunesc__i]='\'
> ((++__tabnlunesc__i))
> continue
> fi
> fi
>
> __tabnlunesc__r[__tabnlunesc__i]=${1:__tabnlunesc__i:1}
> done
> ((__tabnlunesc__i==${#1})) || eval
> __tabnlunesc__r[__tabnlunesc__i]=${1:__tabnlunesc__i:1}
> builtin printf %s "${__tabnlunesc__r[@]}"
> }
>
> On 3/12/21, Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
> > i figured as much :
> >
> > str=a\\ttab\ and\ a\ newline\\nand\ \\backslashes\\$'\n'
> > str=${str//\\\\/$'\1'} str=${str//\\n/$'\n'} str=${str//\\t/$'\t'}
> > str=${str//$'\1'/\\} ; printf %s "$str"
> > a tab and a newline
> > and \backslashes\
> >
> > On Fri, Mar 12, 2021 at 5:40 PM Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com>
> > wrote:
> >
> >> i see
> >> well manual chaining then
> >>
> >> var=b\\c\\tab\\t\\bnewline\\n\\\\n var=${var//\\n/$'\n'}
> >> var=${var//\\t/$'\t'} vvar=${var//\\/\\\\} ; printf %s "$var"
> >>
> >> but that doesnt cover \\ backslashes yet, eg \\n for \n liteeral
> >> no idea of your needs, maybe you can describe in more detail
> >>
> >> On Fri, Mar 12, 2021, 17:07 Peng Yu <pengyu.ut@gmail.com> wrote:
> >>
> >>> No. `printf %b '\a'` prints a bell character. But I still want a slash
> >>> and the character "a". Basically, I only want to treat \\ \n and \t
> >>> specially. All others should be treated literally.
> >>>
> >>> On 3/12/21, Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
> >>> > you may be looking for
> >>> > # printf %b "$str"
> >>> > to interpret the string by prrintf
> >>> >
> >>> > or mass chained singular statements, like you showed or similiar
> >>> >
> >>> > On Fri, Mar 12, 2021, 16:05 Peng Yu <pengyu.ut@gmail.com> wrote:
> >>> >
> >>> >> Hi,
> >>> >>
> >>> >> I wondering if there is a simple but robust way to implement string
> >>> >> escaping.
> >>> >>
> >>> >> Specifically, the string "\n" (a slash and the letter "n") should be
> >>> >> replaced as a newline character, the string "\t" (a slash and the
> >>> >> letter "t") should be replaced as a tab character, and "\\" (two
> >>> >> consecutive slashes should be replaced with a single slash. All
> other
> >>> >> characters and their preceding slash (if there is) should remain as
> >>> >> is.
> >>> >>
> >>> >> If I use a multi-string-replacement strategy, it will not be robust.
> >>> >> For example, if I do it in the order 1) \\ -> \, 2) \n -> newline,
> >>> >> \\n
> >>> >> will not be replaced correctly. The result should be "\n" (a slash
> >>> >> followed by the letter "n").
> >>> >>
> >>> >> $ x='\\n'; x=${x//\\\\/\\}; x=${x//\\n/$'\n'}; declare -p x
> >>> >> declare -- x="
> >>> >> "
> >>> >>
> >>> >> Does anybody have a robust way to implement this in bash?
> >>> >>
> >>> >> --
> >>> >> Regards,
> >>> >> Peng
> >>> >>
> >>> >>
> >>> >
> >>>
> >>>
> >>> --
> >>> Regards,
> >>> Peng
> >>>
> >>
> >
>
>
> --
> Regards,
> Peng
>
- string escaping in bash, Peng Yu, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Peng Yu, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Peng Yu, 2021/03/12
- Re: string escaping in bash,
Alex fxmbsw7 Ratchev <=
- Re: string escaping in bash, Peng Yu, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Peng Yu, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Lawrence Velázquez, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Lawrence Velázquez, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12
- Re: string escaping in bash, Alex fxmbsw7 Ratchev, 2021/03/12