unix2powershell
3 TopicsUnix To PowerShell - Wc
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. wc The Unix “wc” (word count) command will print the character, word, and newline counts for each file specified and a total line if more than one file is specified. This command is useful for quickly scanning a directory for small and large files or to quickly look at a file and determine it’s relative size. The Get-Content Cmdlet will return the number of characters in the full but not the number of lines and words. The following script will emulate the behavior of the Unix “wc” command with a few changes in the way parameters are supplied.3.1KViews0likes2CommentsUnix 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: Unix PowerShell Description FILE -filespec The files to process. -c -characters Output only this range of characters. -f -fields Output only these fields specified by given range. -d -delimiter Use DELIM instead of TAB for input field delimiter. -s -only_delimited Do not print lines not containing delimiters. --output-delimiter -output_delimiter Use STRING as the output deflimiter.2.7KViews0likes4CommentsUnix To PowerShell - Tail
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. tail The Unix “tail” command that is used to display the last 10 lines of each FILE to standard output. With more than one file, precede each with a header giving the file name. There is also a mode where it prints out the last “n” bytes in a file. And for those that want to monitor changes to a file, there is the “follow” option where tail will monitor the file and print out any additions as they are made. I’ve implemented these three options with the follow option only working on line mode. The script could be made to work on byte mode as well, but I’ll leave that to the reader to implement if you really want it. The unix parameters map to the following in my PowerShell script: Unix PowerShell Description -c -num_bytes Output the last N bytes. -n -num_lines Output the last N lines (default 10). -f -follow Output appended data as the file grows. -s -sleep With “-f”, sleep for N seconds between iterations (default 1). -q -quiet Never output headers giving file names. The code will loop through the specified files. For “num line” mode, it will get the contents of the file into a string array and print out the last “n” lines with the default being 10. If the "-follow” switch was given, it will sit in a loop waiting the specified number of seconds before rechecking the file and if any modifications have been made it will print them to the console. This is repeated indefinitely until the script is broken. For byte mode, the content will be loaded into a string and the last “n” characters (up to the size of the file) will be displayed to the console.233Views0likes0Comments