简体   繁体   中英

linux command to get info from a line

I have a file in linux which contains my application's log. With grep I get the wanted lines but I need to process them in order to get only specific value. 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:

grep \\<request\\> myLog.log

However I only want <object> value. 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? There is a better command to do so that 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 -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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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