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".

2 Answers 2

2

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
2
  • Great answer thank you, where did you learn awk in that detail is there any good websites you could recommend? Commented Mar 30, 2017 at 16:00
  • I learned AWK at university... A good resource is the Gawk manual. Commented Mar 30, 2017 at 16:11
2

awk has built in start stop pattern:

awk '/startpattern/,/stoppattern/'
awk '/Name/,/Get-VMProcessor/'
2
  • That includes the line matching the 'stop', which this Q doesn't want. You can add another check like awk '/Name/,/Get-VM/ {if(!/Get-VM/)print}' or you can instead do awk '/Name/,/^$/' or ,!length or ,!NF Commented Apr 21, 2020 at 6:46
  • Yes, indeed I just wanted to point out the start stop way of awk. Which make a lot of task easy and clearer. Commented Apr 22, 2020 at 6:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .