dxx bstu
dxx bstu

Reputation: 33

AWK - match multiple patterns and print results in one line

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?

Upvotes: 1

Views: 3444

Answers (3)

P....
P....

Reputation: 18411

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.

Upvotes: 1

Akshay Hegde
Akshay Hegde

Reputation: 16997

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 

Upvotes: 3

VIPIN KUMAR
VIPIN KUMAR

Reputation: 3147

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

Upvotes: 0

Related Questions