gpt4 book ai didi

linux - awk打印所需数量的字段

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:15 26 4
gpt4 key购买 nike

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

日志文件的格式如上。我只想打印 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

我想打印我正在使用 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

由于 $4 之后的字数不固定,我想打印 $3 和 $5 之后的所有字

我尝试使用以下命令-

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

我想接受 $4 之后的所有单词。

awk 允许这样做吗?

我也希望能使用任何其他命令。

最佳答案

您可以为此使用cut:

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

它打印字段 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"
}

这样调用它:

awk -f script.awk jboss.log

关于linux - awk打印所需数量的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34547804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com