在bash命令中,如何在同一行中打印结果数据?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在bash命令中,如何在同一行中打印结果数据?相关的知识,希望对你有一定的参考价值。

我有下面的命令,我想把结果和delimeters放在同一行。我的命令。

Array=("GET" "POST" "OPTIONS" "HEAD")

echo $(date "+%Y-%m-%d %H:%M")
for i in "${Array[@]}"
do
    cat /home/log/myfile_log | grep "$(date "+%d/%b/%Y:%H")"| awk -v last5=$(date --date="-5 min" "+%M") -F':' '$3>=last5 && $3<last5+5{print}' | egrep -a "$i" | wc -l
done

grep "$(date "+...

2019-01-01 13:27
1651
5760
0
0

It looks (to me) like the overall objective is to scan

2019-01-01 13:27,1651,5760,0,0
for entries that have occurred within the last 5 minutes
答案

, keeping count of the matches along the way and finally printing the current date and the counts to a single line of output./home/log/myfile.logI've opted for a complete rewrite that uses 's abilities of pattern matching, keeping counts and generating a single line of output:NOTE${Array[@]}: The OP may need to revisit the

to handle timestamp periods that span hours, days, months and years, eg, awk,

date1=$(date "+%Y-%m-%d %H:%M")      # current date
date5=$(date --date="-5 min" "+%M")  # date from 5 minutes ago

awk -v d1="${date1}" -v d5="${date5}" -F":" '
BEGIN      { keep=0                  # init some variables
             g=0
             p=0
             o=0
             h=0
           }

$3>=d5 && $3<d5+5 { keep=1 }         # do we keep processing this line?

!keep      { next }                  # if not then skip to next line

/GET/      { g++  }                  # increment our counters
/POST/     { p++  }
/OPTIONS/  { o++  }
/HEAD/     { h++  }
           { keep=0 }                # reset keep flag for next line

                                     # print results to single line of output
END        { printf "%s,%s,%s,%s,%s
", d1, g, p, o, h }
' <(grep "$(date '+%d/%b/%Y:%H')" /home/log/myfile_log)

, etc.Yeah, it's a bit verbose but a bit easier to understand; OP can rewrite<(grep "$(date ...)" /home/log/myfile.log)我想得到下面的结果。14:59 - 16:04 12/31/2019 23:59 - 01/01/2020 00:04

我有下面的命令,我想把结果和delimeters放在同一行。我的命令是: echo $(date "+%Y-%m-%d %H:%M"); for i in "${Array[@]}"; do cat homelogmyfile_log reduce as sees fit.

以上是关于在bash命令中,如何在同一行中打印结果数据?的主要内容,如果未能解决你的问题,请参考以下文章

Lua 打印在同一行

Python print() 函数,在同一行打印

c语言输入的值如何在同一行打印输出

如何在同一行打印2个项目[重复]

在HTML中,如何让两个DIV在同一行显示?

如何让makefile 执行的时候打印所有命令