![KSH는 파일에서 입력을 읽고 같은 줄에 CSV에 씁니다.](https://linux55.com/image/127771/KSH%EB%8A%94%20%ED%8C%8C%EC%9D%BC%EC%97%90%EC%84%9C%20%EC%9E%85%EB%A0%A5%EC%9D%84%20%EC%9D%BD%EA%B3%A0%20%EA%B0%99%EC%9D%80%20%EC%A4%84%EC%97%90%20CSV%EC%97%90%20%EC%94%81%EB%8B%88%EB%8B%A4..png)
다음과 같은 출력이 있고 출력이 다음과 같기를 원합니다.
에서:
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