albciff
albciff

Reputation: 18507

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!!

Upvotes: 0

Views: 132

Answers (3)

Jotne
Jotne

Reputation: 41456

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

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203334

With awk, you can try to match for <object> or </object>, capture it and then print the second column of that capture (behind the first captured <object>):

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

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246774

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

GNU grep with perl-compatible regex

Upvotes: 4

Related Questions