Adam Matan
Adam Matan

Reputation: 136141

awk: Elegant way to fetch the word after a separator

Consider the following DB schema:

CREATE UNIQUE INDEX blah1 ON table1(length);
CREATE INDEX blah2 ON table3(name);
CREATE INDEX blah3 ON table3 USING btree(id);

I want to write a small awk script that would return the names of the indices and tables, e.g.:

 blah1, table1
 blah2, table2
 blah3, table3

I need to match the word that comes after the word INDEX and the word that comes after the word ON and ends with ( or whitespace.

Any idea how to do it using a regex match, without iterating over all $NFs?

Upvotes: 1

Views: 582

Answers (3)

Demosthenex
Demosthenex

Reputation: 4441

Works great with a Perl one-liner.

cat file | perl -ne '/INDEX ([a-zA-Z0-9]+) ON ([a-zA-Z0-9]+)/ && print "$1, $2\n";'

Returns what you wanted.

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 359905

sed 's/.*INDEX \([^ ]*\) ON \([^( ]*\)[( ].*/\1, \2/' inputfile

awk '{sub(".*INDEX ","");sub(" ON ",",");sub("[( ].*",""); sub(",",", ");print}' inputfile

Upvotes: 0

unutbu
unutbu

Reputation: 879143

I don't know awk well, but this seems to work:

awk 'BEGIN{ FS="INDEX | ON "}{gsub(/[ (].*/,"",$3); print $2", " $3}' test.dat

Upvotes: 1

Related Questions