Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oneliner to count the number of tabs in each line of a file

Tags:

powershell

I have a file that is tab delimited. I would like a powershell script that counts the number of tabs in each line. I came up with this:

${C:\tabfile.txt} |% {$_} | Select-String \t | Measure-Object | fl count

it yields 3, Which is the number of lines in the file.

any pointers to what I'm doing wrong? I would like it to print a single number for each line in the file.

like image 895
Jason Horner Avatar asked Aug 11 '09 21:08

Jason Horner


3 Answers

A couple issues with your code, but they all revolve around grouping / array management / nested loops.

gc test.txt | % { ($_ | select-string `t -all).matches | measure | select count }
  • After reading the text file into lines, you need to wrap the rest of the pipeline into a scriptblock. Otherwise downstream cmdlets cannot distinguish which elements came from the "current" line. The PS pipeline is all about dealing with objects one by one -- there's no concept of nested arrays or iterator state or anything else -- blind enumeration.
  • You need to specify -AllMatches, otherwise select-string will stop as soon as it finds the first match on each line. You then need to get the Matches property from its nominal resultset to get the "inner resultset" of this intra-line matching.
like image 116
Richard Berg Avatar answered Oct 18 '22 09:10

Richard Berg


First attempt, not very sophisticated:

gc .\tabfile.txt | % { ($_ -split "`t").Count - 1 }

Utilizing the fact here, that when I split the string at tab characters, I'll get an array with one more item than there are tabs in the line.

Another approach, avoiding splitting the lines:

gc .\tabfile.txt | % { ([char[]] $_ -eq "`t").Count }

Strings can be cast to char[] (also there is the ToCharArray() method), then I am using the fact that comparison operators work differently on collections, by returning all matching items, instead of a boolean. So the comparison there returns an array containing all tabs from the original line from which I just need to get the number of items.

like image 6
Joey Avatar answered Oct 18 '22 10:10

Joey


And yet another option if you are running V2.

select-string \t c:\tabfile.txt -All | 
    %{"$($_.matches.count) tabs on $($_.LineNumber)"}
like image 4
Keith Hill Avatar answered Oct 18 '22 10:10

Keith Hill