我试图通过这个shell命令从Hive输出中获取SQL语句:
tail -f /var/log/hive/hiveserver2.log | grep "Parsing command"
但是,这给了我全文:
2018-03-18 03:07:57,689 INFO [HiveServer2-Handler-Pool: Thread-13816]: parse.ParseDriver (ParseDriver.java:parse(190)) - Parsing command: SELECT * from table
我想grep但是删除前面部分才能显示:
SELECT * from table
有没有办法通过搜索Parsing command:
并在该字符串后保留该行的一部分来在单行中执行此操作?
答案 0 :(得分:1)
您可以将grep
与awk
交换,以获取您正在寻找的线条的子集:
tail -f /var/log/hive/hiveserver2.log |
awk -F "Parsing command: " '{print $2}'
SELECT * from table
-F "Parsing command: "
为"Parsing command: "
命令创建awk
字段分隔符,$2
在此标记后面作为子字符串。
答案 1 :(得分:0)
tail -f /var/log/hive/hiveserver2.log | sed -n '/Parsing command/{s/^.*Parsing command:\s*//;p}'
sed -n
:默认情况下不打印行。/Parsing command/{...}
:对{}
Parsing command
进行操作
s/^.*Parsing command:\s*//
:摆脱从行首(^
)到Parsing command:
的所有内容。也可以删除冒号后面的任何空格(\s*
)p
:打印修改后的行。foo.txt
):2018-03-18 03:07:57,689 INFO [HiveServer2-Handler-Pool: Thread-13816]: parse.ParseDriver (ParseDriver.java:parse(190)) - Parsing command: SELECT * from table
foo
Parsing command: other command
bar
2018-03-18 03:07:57,689 INFO [HiveServer2-Handler-Pool: Thread-13816]: parse.ParseDriver (ParseDriver.java:parse(190)) - Something else
$ sed -n '/Parsing command/{s/^.*Parsing command:\s*//;p}' < foo.txt
SELECT * from table
other command
答案 2 :(得分:0)
实际上你也可以使用grep:
grep -Po 'Parsing command: \K.*'
\K
重置匹配,以便在\K
之后开始。 `-P is for perl regexp, because regular grep regexp doesn't have
\ K .
- o`仅打印匹配的部分。