Bash =~ operator and tab characters

MR ZenWiz mrzenwiz at gmail.com
Fri Apr 22 23:38:28 UTC 2016


On Fri, Apr 22, 2016 at 2:55 PM, Johnny Rosenberg
<gurus.knugum at gmail.com> wrote:
> Hi. I'm kind of struggling with this one.
>
> I need to check a lot of strings for the following:
> TAB ( <at least 4 and not more than 10 numbers or minuses> ) TAB
>
> I tried a lot of variations, I don't even remember them all, but here's one
> of them:
> if [[ ${Rows[$i]} =~ \t[0-9\-]{4,}\t ]]; then
> # Do something
> fi
>
> But so far with no success.
> It SEEMS like the \t doesn't mean the TAB character, but that's not much
> more than a guess.
> What I'm looking for in those strings are a date in parentheses, preceded by
> a TAB and superseded by a TAB. If I now let ↹ represent the TAB character
> and … represent any characters, here are the possible strings I am trying to
> look for with that if statement:
> …↹(2016)↹…
> …↹(2016-04)↹…
> …↹(2016-04-22)↹…
>
> Any thoughts?
> I have done some searching but I always end up with too many hits, and so
> far all of those I looked further into were totally irrelevant, as far as I
> could see.
>

Some thoughts...

I'm not clear if the date you're looking for is in parentheses (as in
your example) or not - your regex doesn't include them, and they'd
need to be escaped for the shell to recognize them properly.

Could you use a word search (I'm not sure if that's a =~ option, but
\< \> works in many places to offset the words).

Any special reason not to use [] (test) or grep or egrep.  In most
cases, I have found them easier to use (and maintain) than regex's.

Have you tried using ^v<tab> to insert the actual tab character in the
pattern?  (You'd probably have to quote the pattern this way...)

Have you quoted the pattern in any way?

Side issue - if you only want 10, you might want to add that as an
upper bound on the {} portion of the pattern.

HTH.
MR



More information about the ubuntu-users mailing list