euro
euro

Reputation: 49

How to check the string length using grep?

I have a lot of Teradata SQL files. The example file look like below:

create multiset volatile table abcde_fghijk_lmnop as(
select 
a.oppnl3_budssstr as nip,
from T45_BACKJJU_33KUT.BRANDFO9 a 
) with data on commit preserve rows;

create multiset volatile table mari_lee as(
select 
b.getter3,
from maleno_fugi75_pratq b
) with data on commit preserve rows;

create multiset table blabla1 as (
select
a.atomic94,
from  b4ty7_manto.pretyu59_bxcx a
) with data on commit preserve rows;

CREATE multiset table blablabla2 AS ( 
SELECT
a.prompter_to12 
FROM tresh_old44 a
) WITH data on commit preserve rows;

CREATE multiset table blablablabla3 AS ( 
SELECT
c.future_opt86 
FROM GFTY_133URO c
) WITH data on commit preserve rows;

I want to create a grep method which can count the length of the table name, which can't exceed 10 signs. I have created a few greps, but none of them work, and I don't know why. What I have done wrong?

for f in /path/to/sql/files/*.sql; do
    if grep -q ' table \{1,10\}' "$f"; then
        echo "correct length of table name $f"
    fi
done

other greps which I used:

if grep -q ' table \{1,10\} as ' "$f"; then
if grep -q ' table \[[:alnum:]]\{1,10\} ' "$f"; then
if grep -q ' table\[[:space:]][[:alnum:]]\{1,10\} ' $f; then

Upvotes: 2

Views: 892

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74595

There are a couple of problems with your attempts. Firstly, it looks like you're escaping the [ in some of your bracket expressions, which means that the [ will be interpreted as a literal character instead. Secondly, you need to take care to match 1 to 10 legal characters, followed by a different character.

This pattern does what you want (I removed the -q so that you can see which table definitions match):

$ grep ' table [[:alnum:]_]\{1,10\}[^[:alnum:]_]' file
create multiset volatile table mari_lee as(
create multiset table blabla1 as (
CREATE multiset table blablabla2 AS (

This pattern matches 1 to 10 alphanumeric characters or underscores, followed by a different character, meaning that the longer table names no longer match.

As it appears that the casing is inconsistent, you should probably also use the -i switch to grep, to enable case-insensitive matching. Otherwise, any definitions that use "TABLE" would not match.

Upvotes: 1

anubhava
anubhava

Reputation: 784998

Use grep with word boundary to list only valid table names:

grep -E 'table +.{1,10}\b' "$f"
create multiset volatile table mari_lee as(
create multiset table blabla1 as (
CREATE multiset table blablabla2 AS (

To suppress output use -q and check return status:

grep -qE 'table +.{1,10}\b' "$f"

Upvotes: 1

Related Questions