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 mat开发者_运维百科ch 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 $NF
s?
I don't know awk well, but this seems to work:
awk 'BEGIN{ FS="INDEX | ON "}{gsub(/[ (].*/,"",$3); print $2", " $3}' test.dat
sed 's/.*INDEX \([^ ]*\) ON \([^( ]*\)[( ].*/\1, \2/' inputfile
awk '{sub(".*INDEX ","");sub(" ON ",",");sub("[( ].*",""); sub(",",", ");print}' inputfile
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.
精彩评论