Bash =~ operator and tab characters
Johnny Rosenberg
gurus.knugum at gmail.com
Sun Apr 24 14:57:10 UTC 2016
2016-04-24 16:31 GMT+02:00 Karl Auer <kauer at biplane.com.au>:
> On Sun, 2016-04-24 at 15:08 +0200, Johnny Rosenberg wrote:
> > I need to check a lot of strings for the following:
> > TAB ( <at least 4 and not more than 10 numbers or minuses> ) TAB
>
> #!/bin/bash
>
> while read LINE ; do
> {
> if [[ $LINE =~ $'\t'[0-9\-]{4,10}$'\t' ]] ; then
> echo "$LINE"
> fi
> }
> done
>
> Redirect or pipe your data into the above script and it will echo all
> matching lines. Not well tested. Note that the regexp does not check
> for formatted dates - just for exactly what you said :-)
>
Well, that's ok. The dates are unfortunately not very consistent. In most
recent cases it's ISO-8601, that is YYYY-MM-DD, but sometimes it's only
YYYY-MM and sometimes even just YYYY.
>
> Obviously you can do whatever else you want inside the if statement.
>
> Oh, and I'm pretty sure the "$'\t'" construct is a bashism.
>
Aaah, yes! I forgot about that one! Thanks for reminding me!
>
> You can't match on tabs nicely in any shell I know of (not that I know
> them all). For heavy duty text processing you are generally better off
> with a language like perl or awk.
>
Unfortunately I don't know much about Perl and I only scratched the surface
of Awk, so I try to use what I know best as much as possible… I know, that
sounds really lazy, but I have other interests in life than just scripting…
:P
Thanks for the help!
Kind regards
Johnny Rosenberg
> Regards, K.
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Karl Auer (kauer at biplane.com.au)
> http://www.biplane.com.au/kauer
> http://twitter.com/kauer389
>
> GPG fingerprint: E00D 64ED 9C6A 8605 21E0 0ED0 EE64 2BEE CBCB C38B
> Old fingerprint: 3C41 82BE A9E7 99A1 B931 5AE7 7638 0147 2C3C 2AC4
>
>
>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20160424/a606fa62/attachment.html>
More information about the ubuntu-users
mailing list