简体   繁体   中英

awk to print required number of fields

07:46:24,059 DEBUG [com.ibm.cmps.portal.web.account.tree.RelationshipRetriever] (http-nykdsr9622/10.54.65.111:4150-3) Fund count: 14
07:46:28,378 DEBUG [com.ibm.cmps.extgrid.grid.StaticHtmlControl] (http-nykcsr5422/10.54.65.111:4150-3) rowCount:75 - displayThreshold:75
07:46:28,384 INFO [com.ibm.cmps.extgrid.xml.TreeGridV6XmlGenerator] (http-nykdsr9622/10.54.65.111:4150-3) Finished layout rendering in 9 ms

Format of Log file is as above. I would like to print only JavaClass Name and Message Log. For example from above text, I need below data to be extracted.

[com.ibm.cmps.portal.web.account.tree.RelationshipRetriever] Fund count: 14 
[com.ibm.cmps.extgrid.grid.StaticHtmlControl] rowCount:75 - displayThreshold:75
[com.ibm.cmps.extgrid.xml.TreeGridV6XmlGenerator] Finished layout rendering in 9 ms

I wish to print I am using awk command to get that. Below are words separated by awk..

$1=07:46:24,059
$2=DEBUG
$3=[com.ibm.cmps.portal.web.account.tree.RelationshipRetriever]
$4=(http-nykdsr9622/10.54.65.111:4150-3)    
$5,$6,.. remaining Log message

As number of words after $4 are not fixed, I wish to print$3 and all words after $5

I tried using below commands-

awk '{print $3, $5;}' jboss.log
awk '{print $3, $5,$6;}' jboss.log

I wish to take all words after $4.

Does awk allows to do that?

I would appreciated usage of any other commands as well.

You can use cut for that:

cut -d' ' -f3,5- jboss.log

It prints field 3 and fields starting from 5 until the end while fields are delimited by a space.


With awk it is a bit more verbose and best explained in multiline version:

script.awk

# on every line
{
    # print field 3
    printf "%s", $3

    # iterate from 5 to number of fields ...
    for(i=5;i<=NF;i++) 
        # ... and print them 
        printf " %s", $i

    # print newline at the end
    printf "\n"
}

Call it like this:

awk -f script.awk jboss.log

will print all but very first column:

awk '{$1=""; print $0}' somefile

will print all but two first columns:

awk '{$1=$2=""; print $0}' somefile

Answer is from here

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