在linux系统中,如何在文件中找到给定单词旁边的单词(紧接着单独出现)?

时间:2016-12-09 11:27:26

标签: linux file unix

我需要在查询日志中的所有select语句中单独查找所有表名。有没有办法grep(或)任何其他选项,以便在我的文件中的单词FROM之后立即获取字符串..

示例:file包含类似

的内容
select a, b, c FROM `table1` join `table2` etc...
insert into.................
commit
select * FROM `tablex` ..............
select y,s,h FROM `tabley`.................
.
.
. 

现在我只想从select stmts中选择不同的表。即,

table1
tablex
tabley

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确但如果你想提取'from'之后的第一个单词,你可以尝试这样的事情:

➜  tmp cat log 
select id, name from account where name = 'John';
select * from price where id > 0;
➜  tmp cat log | gawk -F'from ' '{print $2}' | gawk '{print $1}'
account
price
➜  tmp 

如果您还要打印已连接表格的名称,则需要对其进行扩展。

相关问题