1

Using my limited scripting knowledge.. I have put together an expect script to run some commands on specific devices. The output is below and is saved from all devices to a file (I just listed 2 devices as an example).

  Current State  : Active
Router1slot1# showstats
  Active  stats             : 31
  Active  stats1            : 47
Router1slot1# exit
  Current State  : Standby
Router1slot2# showstats
  Active  stats             : 59
  Active  stats1            : 56
Router1slot2# exit

What I would like is to get some values from the output and have them displayed in rows, delimited by "," :

Router1slot1,Active,31,47
Router1slot2,Standby,59,56

I got a step closer to what I want using :

cat switch.log | awk '/# show/ {print substr($1,1)} /State  :/ {print substr($4,1)} /: / {print substr($5,1)}' >> awk.csv

Output:

Active

Router1slot1#
31
47
Standby

Router1slot2#
59
56

From here I tried different options to convert rows to columns, but it doesn't seem to work. The output is similar to:

56uter1slot2#59

Is there any (more efficient) way to get the required format?

1
  • Hi,Did any of the following answers helped you ? If they does than plese accept/upvote the answer by ticking the right sign besides the answer. If they do not help, please explain what is expected. Commented Mar 24, 2017 at 5:47

3 Answers 3

3

Using given data below one should work, assuming there will not be any empty lines

Input

$ cat file
Current State  : Active
Router1slot1# showstats
  Active  stats             : 31
  Active  stats1            : 47
Router1slot1# exit
  Current State  : Standby
Router1slot2# showstats
  Active  stats             : 59
  Active  stats1            : 56
Router1slot2# exit

Output

$ awk -F'[#:]' -v OFS=, '/exit/{print r,s; r=s=""; next}/showstats/{r=$1;next}{s=(s?s OFS:"")$2}' file
Router1slot1, Active, 31, 47
Router1slot2, Standby, 59, 56

Explanation

awk -F'[#:]' -v OFS=, '             # call awk set and input and ouptut field sep
    /exit/{                         # if exit word found in record
            print r,s;              # print variable r, and s
            r=s="";                 # set variable r and s to null
            next                    # stop processing go to next line
    }
    /showstats/{                    # if showstats found
            r=$1;                   # copy first column to variable r
            next                    # stop processing go to next line
   }
   {
      s=(s?s OFS:"")$2              # variable s contains 2nd field of each line 
                                    # (which is not skipped above using next), 
                                    # if s was set before then concatenate
                                    # s with current record 2nd field, 
                                    # where separator being OFS between 
                                    # them (OFS=output field separator)
   }' file 
Sign up to request clarification or add additional context in comments.

2 Comments

This is very useful, thanks! is there a way to make it work if I would have an empty line after every "Router1slotx# exit" ?
@dxxbstu: just modify {s= to NF{s=
1
awk  ' BEGIN{RS="# exit";OFS=","}{$1=$1} length($0){gsub(/#/,"",$5);print $5,$4,$10,$14}' input
Router1slot1,Active,31,47
Router1slot2,Standby,59,56

This will divide the text into records separated by # exit and then print the selected columns as per requirement. gsub is to remove the pound sign from the slot number field. length($0) is to weed out blank lines from the result.

Comments

0

Try this -

awk -F':' '{ORS=(NR%5==0?RS:FS)}1' f|awk -F'[:# ]+' '{print $5,$4,$9,$12}' OFS=,
Router1slot1,Active,31,47
Router1slot2,Standby,59,56

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.