KSHはファイルから入力を読み取り、同じ行にCSVに書き込みます。

KSHはファイルから入力を読み取り、同じ行にCSVに書き込みます。

次のような出力があり、出力が次のようになります。

から:

GigabitEthernet0/0 is up, line protocol is up
     1 input errors, 0 CRC, 0 frame, 1 overrun, 0 ignored
     275 output errors, 0 collisions, 3 interface resets
GigabitEthernet0/1 is up, line protocol is up
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     42 output errors, 0 collisions, 3 interface resets

受信者:(CSV形式)

Interface, In Errors, Out Errors
GigabitEthernet0/0, 1, 275
GigabitEthernet0/1, 0, 42

上記の内容をforループに入れると、ループの各回転でgrepが行われるため、インターリーブされた出力が得られます。

これは私のスクリプトです。

for line in $(cat $DATAFILE/$rname.intfs)
do
     intf=$(echo $line | grep "line protocol is " | awk '{print $1}')
     inerrs=$(echo $line | grep "input error" | sed 's/^[ \t]*//;s/[ \t]*$//' | awk '{print $1}')
     outerrs=$(echo $line | grep "output error" | sed 's/^[ \t]*//;s/[ \t]*$//' | awk '{print $1}')
     echo "$intf,$inerrs,$outerrs" >intf.csv
done

助けてくれてありがとう。

ありがとう

どう

ベストアンサー1

Awkソリューション(シェルループとパイプチェーンを除く):

awk 'BEGIN{ OFS=", "; print "Interface, In Errors, Out Errors" }
     /line protocol /{ ifc=$1 }
     /input errors/{ in_err=$1 }
     /output errors/{ print ifc, in_err, $1 }' "$DATAFILE/$rname.intfs"

出力:

Interface, In Errors, Out Errors
GigabitEthernet0/0, 1, 275
GigabitEthernet0/1, 0, 42

おすすめ記事