Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Creating and manipulating text has long been one of the primary tasks of scripting languages and  traditional shells. In fact, Perl (the language)

Similar presentations


Presentation on theme: " Creating and manipulating text has long been one of the primary tasks of scripting languages and  traditional shells. In fact, Perl (the language)"— Presentation transcript:

1

2  Creating and manipulating text has long been one of the primary tasks of scripting languages and  traditional shells. In fact, Perl (the language) started as a simple (but useful) tool designed for text  processing. It has grown well beyond those humble roots, but its popularity provides strong evidence of  the need it fills.  In text-based shells, this strong focus continues. When most of your interaction with the system happens  by manipulating the text-based output of programs, powerful text processing utilities become crucial.  These text parsing tools such as awk, sed, and grep form the keystones of text-based systems  management.  In PowerShell's object-based environment, this traditional tool chain plays a less critical role. You can  accomplish most of the tasks that previously required these tools much more effectively through other  PowerShell commands. However, being an object-oriented shell does not mean that PowerShell drops all  support for text processing. Dealing with strings and unstructured text continues to play an important  part in a system administrator's life. Since PowerShell lets you to manage the majority of your system in  its full fidelity (using cmdlets and objects), the text processing tools can once again focus primarily on  actual text processing tasks.

3  Use PowerShell string variables to give you a way to store and work with text.  To define a string that supports variable expansion and escape characters in its definition, surround it  with double quotes:  $myString = "Hello World"  To define a literal string (that does not interpret variable expansion or escape characters), surround it  with single quotes:  $myString = 'Hello World'  String literals come in two varieties: literal (nonexpanding) and expanding strings. To create a literal  string, place single quotes ($myString = 'Hello World') around the text. To create an expanding string,  place double quotes ($myString = "Hello World") around the text.  In a literal string, all the text between the single quotes becomes part of your string. In an expanding  string, PowerShell expands variable names (such as $myString) and escape sequences (such as `n) with  their values (such as the content of $myString and the newline character, respectively).  For a detailed explanation of the escape sequences and replacement rules inside PowerShell strings  One exception to the "all text in a literal string is literal" rule comes from the quote characters  themselves. In either type of string, PowerShell lets you to place two of that string's quote characters  together to add the quote character itself:

4  $myString = "This string includes ""double quotes"" because it combined quote  characters."  $myString = 'This string includes ''single quotes'' because it combined quote  characters.'  This helps prevent escaping atrocities that would arise when you try to include a single quote in a singlequoted  string. For example:  $myString = 'This string includes ' + "'" + 'single quotes' + "'"

5  Use a PowerShell here string to store and work with text that includes newlines and other formatting  information.  $myString = @"  This is the first line  of a very long string. A "here string"  lets you to create blocks of text  that span several lines.  "@  PowerShell begins a here string when it sees the characters @" followed by a newline. It ends the string  when it sees the characters "@ on their own line. These seemingly odd restrictions let you create strings  that include quote characters, newlines, and other symbols that you commonly use when you create  large blocks of preformatted text.  Like string literals, here strings may be literal (and use single quotes) or expanding (and use double  quotes).

6  In an expanding string, use PowerShell's escape sequences to include special characters such as tab and  newline.  PS > $myString = "Report for Today`n----------------"  PS > $myString  Report for Today

7  In an expanding string, include the name of a variable in the string to insert the value of that variable.  PS > $header = "Report for Today"  PS > $myString = "$header`n----------------"  PS > $myString  Report for Today  ----------------  To include information more complex than just the value of a variable, enclose it in a subexpression:  PS > $header = "Report for Today"  PS > $myString = "$header`n$('-' * $header.Length)"  PS > $myString  Report for Today  ---------------- Variable substitution in an expanding string is a simple enough concept, but subexpressions deserve a  little clarification.  A subexpression is the dollar sign character, followed by a PowerShell command (or set of commands)  contained in parentheses:  $(subexpression)

8  When PowerShell sees a subexpression in an expanding string, it evaluates the subexpression and places  the result in the expanding string. In the solution, the expression '-' * $header.Length tells PowerShell  to make a line of dashes $header.Length long.  Another way to place dynamic information inside a string is to use PowerShell's string formatting  operator, which is based on the rules of the.NET string formatting:  PS > $header = "Report for Today"  PS > $myString = "{0}`n{1}" -f $header,('-' * $header.Length)  PS > $myString

9  Use PowerShell's formatting operator to place formatted information inside a string.  PS > $formatString = "{0,8:D4} {1:C}`n"  PS > $report = "Quantity Price`n"  PS > $report += "---------------`n"  PS > $report += $formatString -f 50,2.5677  PS > $report += $formatString -f 3,9  PS > $report  Quantity Price  ---------------  0050 $2.57  0003 $9.00  s  PowerShell's string formatting operator (-f) uses the same string formatting rules as the String.Format()  method in the.NET Framework. It takes a format string on its left side, and the items you want to format  on its right side.  In the solution, you format two numbers: a quantity and a price. The first number ({0}) represents the  quantity and is right-aligned in a box of 8 characters (,8). It is formatted as a decimal number with 4  digits (:D4). The second number ({1}) represents the price, which you format as currency (:C).

10  Wilson, E. (2007). Microsoft Windows PowerShell step by step. Washington: Microsoft Press.  Tomsho, G. (2010). MCTS guide to Microsoft Windows Server 2008 Active Directory configuration: Exam 70-640. Boston, MA: Course Technology/Cengage Learning.  Schwichtenberg, H. (2008). Essential PowerShell. The Addison-Wesley Microsoft technology series. Upper Saddle River, NJ: Addison-Wesley.


Download ppt " Creating and manipulating text has long been one of the primary tasks of scripting languages and  traditional shells. In fact, Perl (the language)"

Similar presentations


Ads by Google