liveBook · Manning

Parsing

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

Text parsing is the process of analyzing textual content to identify, extract, or transform structured information.

Text parsing analyzes textual output to find, extract, or transform useful information. Administrators may parse command output or log files, but formatting changes can break commands that depend on column positions. section 8.2 section 21.0

Common approaches include:

  • Use grep, awk, sed, or regular expressions to identify patterns in text. section 8.2
  • Use PowerShell objects when possible, addressing named properties instead of parsing displayed column positions. section 8.2
  • Use regular expressions in PowerShell when textual content, such as a log file, still needs parsing. section 21.0

Extracting process identifiers

A process listing displays several columns, including process names and IDs. Text-based processing can select matching rows and extract identifiers for a later administrative action; PowerShell objects reduce dependence on the displayed layout. section 8.2

PS /Users/travis> Get-Process
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     39       5     1876       4340    52    11.33   1920 Code
     31       4      792       2260    22     0.00   2460 Code
     29       4      828       2284    41     0.25   3192 Code
    574      12     1864       3896    43     1.30    316 pwsh
    181      13     5892       6348    59     9.14    356 ShipIt
    306      29    13936      18312   139     4.36   1300 storeaccountd
    125      15     2528       6048    37     0.17   1756 WifiAgent
   5159    7329    85052      86436   118     1.80   1356 WifiProxy

Inspect the process listing to see the tabular text that may be searched, filtered, or converted into selected identifiers. section 8.2

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Contrasts text parsing with PowerShell objects and introduces regular expressions without covering regex comprehensively.
Examples
Uses process listings, process identifiers, command output, and log files as parsing examples.
What you can do after
Apply it via regular expressions and shell text tools to extract information from command output or log files.
How it connects
Connects parsing with PowerShell objects, regular expressions, Bash, grep, awk, sed, Perl, and Python.

Also explained in

Compare how 13 books explain parsing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.