Bash =~ operator and tab characters

Karl Auer kauer at biplane.com.au
Sun Apr 24 14:31:43 UTC 2016


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 :-)

Obviously you can do whatever else you want inside the if statement.

Oh, and I'm pretty sure the "$'\t'" construct is a bashism.

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.

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






More information about the ubuntu-users mailing list