Section 5.2. Work with Text Files


5.2. Work with Text Files

We've already seen how scripts and functions can be captured in MSH script files and saved to disk for later use. Now, it's time to take a look at the results they produce and how those, too, can be persisted to a permanent store. Unlike most other shells, MSH works seamlessly with several different input and output formats, including simple text, comma-separated variable lists, and even XML representation: the data interchange format is no longer dictated by the toolset! We'll start by looking at data storage in text files and move on to other structured formats in the next section.

5.2.1. How Do I Do That?

get-content is a simple cmdlet that reads from the filesystem and puts each line of content (in the form of a string) into the pipeline in sequence. Its behavior is so similar to the type command in cmd.exe (and the cat command in Unix shells) that it is aliased with both terms by default. Both type and get-content can be used interchangeably in the examples that follow, but we'll use get-content for consistency. The first couple of examples use a simple text file that contains the familiar sentence "The quick brown fox jumped over the lazy dog," separated across three lines, as shown below:

     MSH D:\MshScripts> get-content brownfox.txt     The quick brown fox     jumped over     the lazy dog

As we saw at the end of Chapter 3, it's convenient to pipe the output of get-content into other cmdlets. Let's revisit the versatile measure-object cmdlet, but this time, let's use some options that are designed precisely for sequences of lines such as those in a text file: -lines, -words, and -characters:

     MSH D:\MshScripts> get-content brownfox.txt | measure-object -lines -words     -characters     Lines               Words               Characters          Property     -----               -----               ----------          --------     3                   9                   42

out-file is used to make changes to files stored on the filesystem. When given a filename, it will use the default formatter to output the contents of the pipeline to the filesystem:

     MSH D:\MshScripts> get-process | out-file raw.txt     MSH D:\MshScripts> get-content raw.txt     Handles  NPM(K)    PM(K)      WS(K) VS(M)   CPU(s)     Id ProcessName     -------  ------    -----      ----- -----   ------     -- -----------         672      12    11768      10956    65   184.73   1520 CcmExec          24       1     1412       1440    13     0.04   3412 cmd         462       5     1736       2156    25   114.71    352 csrss         256       5     2428       7376    30    28.33   2932 dllhost         433      15     8820      12076    64    90.73   2260 explorer           0       0        0         16     0               0 Idle     ...

5.2.2. What About ...

... Controlling scrolling when a text file is too long to fit on a single screen? The MORE command in cmd.exe is used to page output, waiting for a key press at the end of each screen of information. This functionality is carried over into MSH in the form of a standard function. Feel free to put more at the end of the pipeline to pace the output: get-content longfile.txt | more.

5.2.3. Where Can I Learn More?

The get-help system has more details on the get-content and out-file cmdlets. For more information on using other data formats, keep reading.




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net