내 변수는 다음과 같습니다.
HEADER='Device bps tps bread bwrtn'
HEADERIZE="BEGIN {print \"$HEADER\"}"
PRINTF='{printf "%-10s %11s %11s %12s %12s %13s %13s %13s\n", device, bps, tps, bread, bwrtn}'
CMD='iostat -DlR 1 2'
FILTER='/^cd/ {next} /^Disks:/ {reportOrd++; next} (reportOrd<2) {next}'
FORMAT='{device=$1; bps="?"; tps="?"; bread=$5; bwrtn=$6}'
$CMD | awk "$HEADERIZE $FILTER $FORMAT $PRINTF" header="$HEADER"
출력이 다음과 같기를 원합니다.
Device bps tps bread bwrtn
hdisk0 0.0 0.0 0.0 0.0
hdisk1 0.0 0.0 0.0 0.0
hdisk2 0.0 0.0 0.0 0.0
hdisk3 0.0 0.0 0.0 0.0
물론 더 많은 성능 헤더를 원했지만 단순하게 유지하려고 노력했습니다.
다음을 실행할 때 문제가 발생합니다 $CMD | awk "$HEADERIZE $FILTER $FORMAT $PRINTF" header="$HEADER"
. 결과는 다음과 같습니다.
Device bps tps bread bwrtn
--------------- ? ? --------------------------------------
%tm ? ? bwrtn rps
act ? ? outs serv
hdisk1 ? ? 0.0 0.0
hdisk0 ? ? 0.0 0.0
변수 "FILTER"가 잘못된 것 같지만 올바른 구문을 모릅니다.
입력은 다음과 같습니다.iostat -DlR 1 2
산출:
root@myserver #iostat -DlR 1 2
System configuration: lcpu=20 drives=2 paths=16 vdisks=0
Disks: xfers read write queue
--------------- -------------------------------- ------------------------------------ ------------------------------------ --------------------------------------
%tm bps tps bread bwrtn rps avg min max time fail wps avg min max time fail avg min max avg avg serv
act serv serv serv outs serv serv serv outs time time time wqsz sqsz qfull
hdisk1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0.0 0.0 0.0 0.0 0 0 0.0 0.0 0.0 0.0 0.0 0.0
hdisk0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0.0 0.0 0.0 0.0 0 0 0.0 0.0 0.0 0.0 0.0 0.0
Disks: xfers read write queue
--------------- -------------------------------- ------------------------------------ ------------------------------------ --------------------------------------
%tm bps tps bread bwrtn rps avg min max time fail wps avg min max time fail avg min max avg avg serv
act serv serv serv outs serv serv serv outs time time time wqsz sqsz qfull
hdisk1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0.0 0.0 0.0 0.0 0 0 0.0 0.0 0.0 0.0 0.0 0.0
hdisk0 0.0 77.8K 19.0 0.0 77.8K 0.0 0.0 0.0 0.0 0 0 19.0 0.3 0.2 0.5 0 0 0.0 0.0 0.0 0.0 0.0 0.0
Desired output:
Device bps tps bread bwrtn
hdisk0 0.0 0.0 0.0 0.0
hdisk1 0.0 0.0 0.0 0.0
hdisk2 0.0 0.0 0.0 0.0
hdisk3 0.0 0.0 0.0 0.0
답변1
해결책:
iostat -d 1 2 | awk 'BEGIN{print "Device\tbps\ttps\tbread\tbwrtn"}/^hdisk/{print $1"\t"$3"\t"$4"\t"$5"\t"$6}'
산출:
Device bps tps bread bwrtn
hdisk1 0.0 0.0 0.0 0.0
hdisk0 0.0 0.0 0.0 0.0
hdisk1 0.0 0.0 0.0 0.0
hdisk0 77.8K 19.0 0.0 77.8K
논평:
일을 너무 복잡하게 만드는 것 같아 걱정됩니다. 제공된 솔루션은 입력 샘플과 원하는 출력을 기반으로 한 추측이며 일부 불일치가 있습니다. hdisk2
데이터 및 의 출처 hdisk3
가 입력에 없으므로 설명해주세요 . 또한 코드를 여러 환경 변수로 분할하는 복잡한 솔루션을 구축하는 이유도 설명해주세요.