Unix To PowerShell – Cut

PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on Unix based tools to get their job done.  In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.

cut

The Unix “cut” command is used to extract sections from each link of input.  Extraction of line segments can be done by bytes, characters, or fields separated by a delimiter.  A range must be provided in each case which consists of one of N, N-M, N- (N to the end of the line), or –M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value).

For PowerShell, I’ve omitted support for bytes but the rest of the features is included.  The Parse-Range function is used to parse the above range specification.  It takes as input a range specifier and returns an array of indices that the range contains.  Then, the In-Range function is used to determine if a given index is included in the parsed range. 

The real work is done in the Do-Cut function.  In there, input error conditions are checked.  Then for each file supplied, lines are extracted and processed with the given input specifiers.  For character ranges, each character is processed and if it’s index in the line is in the given range, it is appended to the output line.  For field ranges, the line is split into tokens using the delimiter specifier (default is a TAB).  Each field is processed and if it’s index is in the included range, the field is appended to the output with the given output_delimiter specifier (which defaults to the input delimiter).

The options to the Unix cut command are implemented with the following PowerShell arguments:

UnixPowerShellDescription
FILE-filespecThe files to process.
-c-charactersOutput only this range of characters.
-f-fieldsOutput only these fields specified by given range.
-d-delimiterUse DELIM instead of TAB for input field delimiter.
-s-only_delimitedDo not print lines not containing delimiters.
--output-delimiter-output_delimiterUse STRING as the output deflimiter.

 

Published May 06, 2009
Version 1.0

Was this article helpful?

4 Comments

  • @UnixUser - Great point. PowerShell has been a big jump from Command.exe and the rich integration with the new server platforms will make it more prevalent in the future. I too rely on my set of unix tools (grep, curl, wget, etc) and that is what this series of posts was geared to address.

     

     

    Again, thanks for the great feedback and if you do take a look at some of these scripts I've written, I'd love to hear feedback on their implementation/usage.

     

     

    Cheers!

     

     

    -Joe
  • That should work, are you seeing that it isn't? If you could post a few lines of your test.csv file, I'll test it out.

     

     

    -Joe
  • I think your issue isn't with the passing in of the command but rather with how your command is formulated. When I tried to enter just the "get-wmiobject ..." directly into powershell I got the same error. The issue is with the code after the second pipe. I'd try something like this:

     

     

    "Get-WMIObject -class MSFT_SPIESUserSetting | where-object { $_.PrimaryURI -eq 'sip:Navin@OCSR2.COM' } | foreach-object -process { $_.Enabled = $True; $_.put() | out-null; }"

     

     

    I've put a foreach-object before your last expression block and it seems to work for me.

     

     

    Let me know how it goes...

     

     

    -Joe
  • @sevenforce, thanks for the fix, I'll test it out and get the post updated.

     

     

    -Joe