awk打印所需的字段数

Ume*_*til 0 linux bash awk

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
Run Code Online (Sandbox Code Playgroud)

日志文件的格式如上.我想只打印JavaClass名称和消息日志.例如,从上面的文字,我需要提取下面的数据.

[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
Run Code Online (Sandbox Code Playgroud)

我希望打印我使用awk命令来获得它.下面是由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
Run Code Online (Sandbox Code Playgroud)

由于$ 4之后的单词数量不固定,我希望在$ 5之后打印$ 3和所有单词

我尝试使用以下命令 -

awk '{print $3, $5;}' jboss.log
awk '{print $3, $5,$6;}' jboss.log
Run Code Online (Sandbox Code Playgroud)

我想在4美元之后接受所有的话.

awk允许这样做吗?

我也很感激使用任何其他命令.

hek*_*mgl 5

你可以用cut它:

cut -d' ' -f3,5- jboss.log
Run Code Online (Sandbox Code Playgroud)

它打印字段3和从5开始直到结束的字段,而字段由空格分隔.


使用awk,它在多行版本中更加冗长和最佳解释:

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"
}
Run Code Online (Sandbox Code Playgroud)

像这样称呼它:

awk -f script.awk jboss.log
Run Code Online (Sandbox Code Playgroud)