0

I have the following output and want the output to look:

FROM:

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

TO: (in CSV format)

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

When I put the above in a for loop I get staggered outputs as Im grepping on the turn of every loop

Heres my script

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

Any help is appreciated

thanks

Jay

3
  • Formatting doesnt show correct FROM: 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 TO: (in CSV format) Interface, In Errors, Out Errors GigabitEthernet0/0, 1, 275 GigabitEthernet0/1, 0, 42 Commented Feb 15, 2018 at 11:12
  • Use the curly braces:{} Commented Feb 15, 2018 at 11:15
  • See unix.stackexchange.com/help/formatting Commented Feb 15, 2018 at 11:18

1 Answer 1

0

Awk solution (without shell loops and chains of pipelines):

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"

The output:

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.