简体   繁体   English

linux命令从一行中获取信息

[英]linux command to get info from a line

I have a file in linux which contains my application's log. 我在linux中有一个文件,其中包含应用程序的日志。 With grep I get the wanted lines but I need to process them in order to get only specific value. 使用grep,我得到了所需的行,但是我需要对其进行处理以便仅获得特定的值。 More exactly I have the next log: 更确切地说,我有下一个日志:

13 Jan 2014 15:22:18,291 DEBUG some data
13 Jan 2014 15:22:18,291 DEBUG some data
13 Jan 2014 15:22:18,291 DEBUG <request><object>3</object></request>
13 Jan 2014 15:22:18,291 DEBUG <request><object>4</object></request>
13 Jan 2014 15:22:18,291 DEBUG <request><object>5</object></request>
13 Jan 2014 15:22:18,291 DEBUG more data

With the next command I get the log lines with the XML: 使用下一条命令,我将获得带有XML的日志行:

grep \\<request\\> myLog.log

However I only want <object> value. 但是我只想要<object>值。 Normally I make this kind of things with awk however I only use this command to work with lines which has columns and I don't know how to achieve this, can someone put me on the right direction? 通常,我使用awk进行此类操作,但是我仅使用此命令来处理具有列的行,而且我不知道如何实现这一点,有人可以向我指出正确的方向吗? There is a better command to do so that awk ? 有一个更好的命令去做,让awk

Thanks!! 谢谢!!

grep -oP '<request><object>\K[^<]*' file

具有与Perl兼容的正则表达式的GNU grep

You can do: 你可以做:

awk -F"[<>]" '/<request>/ {print $5}' file
3
4
5

If number of field may vary, then this awk prints only value after <object> 如果字段数可能有所不同,则此awk仅在<object>之后打印值

awk -F"><object>" '/<request>/ {split($2,a,"<");print a[1]}' file
3
4
5

Or like this: 或像这样:

awk -F"><object>" '/<request>/ {print $2+0}' file
3
4
5

使用awk ,您可以尝试匹配<object>或</ object>,捕获它,然后打印该捕获的第二列(在第一个捕获的<object>后面):

$ awk -F'</?object>' 'NF>1{print $2}' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM