1

我目前有一个 shell 脚本,其中有一段代码运行远程 shell 命令,如下所示:

grep -v "^[     ]*#" ${CONFIG_FILE} |
while read SERVER USER LOGDIR
do
{
echo "Stats for" $date
echo "${USER}"
echo "iTunes:"
time /bob/shared/ted/remote/rssh ${SERVER} "gzgrep -c \"iTunes Music\" ${LOGDIR}/*${date}*.gz | awk '-F:' '{total+=\$NF}END{print \"Total=\" total}'" 2>&1
echo
echo "Default"
time /bob/shared/ted/remote/rssh ${SERVER} "gzgrep -c \"Default Player\" ${LOGDIR}/*${date}*.gz | awk '-F:' '{total+=\$NF}END{print \"Total=\" total}'" 2>&1
echo
echo "The Rest"
time /bob/shared/ted/remote/rssh ${SERVER} "gzgrep -c \"Junk\"     ${LOGDIR}/*${date}*.gz | awk '-F:' '{total+=\$NF}END{print \"Total=\" total}'" 2>&1
} </dev/null 2>&1

done 2>${ERROR_FILE} >${STATS_FILE}

这给了我这样的输出:

Stats for 20120628
Rich
iTunes:
Total=0

real 0m2.09s
user 0m0.22s
sys  0m0.03s

Default:
Total=0

real 0m2.03s
user 0m0.23s
sys 0m0.00s

The Rest:
Total=0

real 0m2.03s
user 0m0.23s
sys 0m0.00s

Stats for 20120628
Ian
iTunes:
Total=2

real 0m21.44s
user 0m0.22s
sys 0m0.00s

Default:
Total=2

real 0m21.48s
user 0m0.21s
sys 0m0.02s

The Rest:
Total=0

real 0m2.03s
user 0m0.23s
sys 0m0.00s

等等等等……

我想要的是像这样以 CSV 格式输出:

Rich,0,0,0
Ian,2,2,0

修改上述代码以获得我需要的输出格式的最简单方法是什么?

谢谢!

4

1 回答 1

1
grep -v "^[     ]*#" ${CONFIG_FILE} |
while read SERVER USER LOGDIR
do
{
echo "${USER},"
time /bob/shared/ted/remote/rssh ${SERVER} "gzgrep -c \"iTunes Music\" ${LOGDIR}/*${date}*.gz | awk '-F:' '{total+=\$NF}END{print total \",\"}'" 2>&1
time /bob/shared/ted/remote/rssh ${SERVER} "gzgrep -c \"Default Player\" ${LOGDIR}/*${date}*.gz | awk '-F:' '{total+=\$NF}END{print total \",\"}'" 2>&1
time /bob/shared/ted/remote/rssh ${SERVER} "gzgrep -c \"Junk\"     ${LOGDIR}/*${date}*.gz | awk '-F:' '{total+=\$NF}END{print total}'" 2>&1
} </dev/null 2>&1 | tr -d '\n'

done 2>${ERROR_FILE} >${STATS_FILE}

我删除了多余的回声,附加了逗号并确保它在一行上。尝试一下。

如果您也不想要时间信息,只需删除所有time调用。

于 2012-07-02T16:56:10.257 回答