William
William

Reputation: 13

Awk multiple pattern matching and formatting the output in to columns from an input file

I'm gathering stats in a single file on 11 processes and how much memory each one uses at 5 minute intervals.

I'm trying to use awk to multiple pattern match then format the output in to another file so I can import that in to excel to produce a trend analysis.

Each of the lines have 11 unique patterns for example here's the current format:

06/09/20 13:30:00 240.73 MB PATTR1
06/09/20 13:30:00 66.28 MB PATTR2
06/09/20 13:30:00 25.26 MB PATTR3
06/09/20 13:30:00 25.12 MB PATTR4
06/09/20 13:30:00 18.43 MB PATTR5
06/09/20 13:30:00 15.82 MB PATTR6
06/09/20 13:30:00 7.69 MB PATTR7
06/09/20 13:30:00 7.34 MB PATTR8
06/09/20 13:30:00 6.08 MB PATTR9
06/09/20 13:30:00 3.86 MB PATTR10
06/09/20 13:30:00 3.84 MB PATTR11
06/09/20 13:35:01 240.88 MB PATTR1
06/09/20 13:35:01 73.31 MB PATTR2
06/09/20 13:35:01 25.26 MB PATTR3
06/09/20 13:35:01 25.12 MB PATTR4
06/09/20 13:35:01 18.43 MB PATTR5
06/09/20 13:35:01 15.82 MB PATTR6
06/09/20 13:35:01 7.69 MB PATTR7
06/09/20 13:35:01 7.34 MB PATTR8
06/09/20 13:35:01 6.08 MB PATTR9
06/09/20 13:35:01 3.86 MB PATTR10
06/09/20 13:35:01 3.84 MB PATTR11

I can match all the patterns using awk but unsure how to code awk to get the desired output?

awk '/PATTR1/ || /PATTR2/ || /PATTR3/ || /PATTR4/ || /PATTR5/ || /etc../' inputfile

What I'm trying to achieve is:

  1. Match the 11 unique patterns.
  2. Display the data from column 3 on the input file.
  3. Format the ouput in 11 column with each column labeled w the respective pattern.

Desired ouput:

PATTR1  PATTR2  PATTR3  PATTR4  PATTR5  PATTR6  PATTR7  PATTR8  PATTR9  PATTR10 PATTR11
66.28   240.73  25.12   25.26   18.43   15.82   7.69    7.34    3.86    3.84    6.08
73.31   240.88  25.12   25.26   18.43   15.82   7.69    7.34    3.86    3.84    6.08
80.31   240.96  25.12   25.26   18.43   15.82   7.69    7.34    3.86    3.84    6.08
87.24   241.07  25.12   25.26   18.43   15.82   7.69    7.34    3.86    3.84    6.08
94.05   241.19  25.12   25.26   18.43   15.82   7.69    7.34    3.86    3.84    6.08
100.85  241.31  25.12   25.26   18.43   15.82   7.69    7.34    3.86    3.84    6.08
108     241.44  25.12   25.26   18.43   15.82   7.69    7.34    3.86    3.84    6.08

Any help would be greatly appreciated :)

Upvotes: 1

Views: 239

Answers (1)

Ed Morton
Ed Morton

Reputation: 203502

I think this is what you might be trying to do but I'm not sure because idk why you're trying to match all those "PATTR"s when they appear in every line of input and the expected output you provided doesn't seem to match the sample input you provided and you say you want a CSV to import to Excel but then your expected output doesn't contain any commas:

$ awk '
    { hdr=hdr sep $NF; vals=vals sep $3; sep="\t" }
    !(NR%11) { if (NR==11) print hdr; print vals; hdr=vals=sep="" }
' file
PATTR1  PATTR2  PATTR3  PATTR4  PATTR5  PATTR6  PATTR7  PATTR8  PATTR9  PATTR10 PATTR11
240.73  66.28   25.26   25.12   18.43   15.82   7.69    7.34    6.08    3.86    3.84
240.88  73.31   25.26   25.12   18.43   15.82   7.69    7.34    6.08    3.86    3.84

If you do want a CSV instead of TSV then just change sep="\t" to sep=",". If that's not all you need then update your question to provide more truly representative and consistent sample input/output that better captures your requirements.

Upvotes: 1

Related Questions