0

What I am trying to do is use awk to match a pattern, print certain columns and stop when it finds a different pattern.

For example

I have a file that contains the following:

Enter-PSSession -ComputerName ACD-VMH04 -Credential afornito

Name       State   CPUUsage(%) MemoryAssigned(M) Uptime     Status
----       -----   ----------- ----------------- ------     ------
ACD-CMS03  Running 0           4096              9.14:40:30 Operating normally
ACD-FFS01  Running 0           8192              9.14:31:22 Operating normally
ACD-FS01   Running 0           3072              9.14:31:28 Operating normally
ACD-WP01   Running 15          4096              9.14:31:33 Operating normally
ACP-DEV-01 Running 0           4096              9.14:31:13 Operating normally
VMDEV01    Running 0           2048              9.14:31:48 Operating normally

Get-VMProcessor |  Select-Object VMName, Count

VMName                                                                                                            Count
------                                                                                                            -----
ACD-CMS03                                                                                                             2
ACD-FFS01                                                                                                             1
ACD-FS01                                                                                                              2
ACD-WP01                                                                                                              2
ACP-DEV-01                                                                                                            2
VMDEV01                                                                                                               2

Get-VMMemory

VMName     DynamicMemoryEnabled Minimum(M) Startup(M) Maximum(M)
------     -------------------- ---------- ---------- ----------
ACD-CMS03  False                512        4096       1048576
ACD-FFS01  False                512        8192       1048576
ACD-FS01   False                512        3072       1048576
ACD-WP01   False                512        4096       1048576
ACP-DEV-01 False                512        4096       1048576
VMDEV01    True                 2048       2048       4096

Now I want to print only the name, state, memoryassigned and status from the first couple rows

Each file is the same as far as layout so the first one is simple to start

awk 'NR>=5&&NR<=10 {print $1, $2, $4, $5}' stats.txt

What I do not want is the "<=10" part what can i use to stop when it sees "Get-VMProcessor" instead?

Second part how can I start printing when finds "Select-Object VMName, Count" skip 3 lines and print until it finds "Get-VMMemory".

0

There are a number of techniques.

If you want to just stop processing when you see "Get-VMProcessor", you can exit:

awk '/Get-VMProcessor/ { exit }; NR>=5 { print $1, $2, $4, $5 }' stats.txt

A more generic solution is to use an output flag:

awk 'NR == 5 { output = 1 }; /Get-VMProcessor/ { output = 0 }; output { print $1, $2, $4, $5 }' stats.txt

You can combine that with a start line to handle your second block:

awk 'NR == 5 { output = 1 }; /Get-VMProcessor/ || /Get-VMMemory/ { output = 0 }; /Select-Object VMName, Count/ { output = 1; start = NR + 4 }; output && NR >= start { print $1, $2, $4, $5 }' stats.txt
  • Great answer thank you, where did you learn awk in that detail is there any good websites you could recommend? – Anthony Fornito Mar 30 '17 at 16:00
  • I learned AWK at university... A good resource is the Gawk manual. – Stephen Kitt Mar 30 '17 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.