0

I'm trying to parse through some massive SQL query logs using sed and want to extract the names of all the tables that are joined in each query. Here's what I'm trying:

echo '[SQL_STMT="SELECT A FROM TABLEA JOIN TABLEB ON SOMETHING JOIN TABLEC ON SOMETHINGELSE WHERE A = 1"]' \
| sed -e 's/SQL_STMT=.*JOIN \([A-Z0-9_]*\) .*\]$/SQL_JOINS="\1"]/g'

but this only returns the following:

[SQL_JOINS="TABLEC"]

what I'd like to see is something like this:

[SQL_JOINS="TABLEB TABLEC"]

and have it work for any number of joins

So how do I structure the backreference so that it gets all the joined tables?

4

1 回答 1

0

如果你对 perl 解决方案没问题,试试这个:

$ echo "......" | perl -ne '@x=/JOIN (\w+)/g;print "[SQL_JOINS=\""."@x"."\"]";'

在上面的测试中:

$ echo "[SQL_STMT=\"SELECT A FROM TABLEA JOIN TABLEB ON SOMETHING JOIN TABLEC ON SOMETHINGELSE WHERE A = 1\"]" | perl -ne '@x=/JOIN (\w+)/g;print "[SQL_JOINS=\""."@x"."\"]";'
[SQL_JOINS="TABLEB TABLEC"]

sed解决方案:

echo ...|sed -n ':a;/JOIN/{s/[^J]*JOIN \([A-Z0-9_]*\)/\1\n/;P;D;ta}' | sed -e ':a;N;s/\n/ /;ta' | sed  's/.*/[SQL_JOINS=\"&\"]/'

第一个 sed 打印所有表名,第二个将表名的所有行合并为一个,最后一个将其格式化为所需的格式。

于 2012-10-26T15:13:59.490 回答