5

I have a simple text file:

$ cat food.txt
Apples
Bananas
Carrots

Linux/Cygwin

And I can think of several ways to get line numbers in Linux/Cygwin:

$ nl food.txt
     1  Apples
     2  Bananas
     3  Carrots

$ cat -n food.txt
     1  Apples
     2  Bananas
     3  Carrots

$ less -NFX food.txt
      1 Apples
      2 Bananas
      3 Carrots

PowerShell

The best I've come up with is this:

Update 2017-11-27Mo: (1) Slight tweak added: Out-String -Stream to forcibly textify those pesky objects. (2) Note: I'm looking for something that will accept PIPELINE INPUT.

PS C:\> function nl{$input | Out-String -Stream | Select-String '.*' | Select-Object LineNumber, Line}

PS C:\> cat .\food.txt | nl
LineNumber Line
---------- ----
         1 Apples
         2 Bananas
         3 Carrots

Is there a simpler way? Shorter? Something builtin with PowerShell?

  • 1
    cat .\food.txt | select @{ n='LineNumber'; e='ReadCount' }, @{ n='Line'; e={$_} } – user364455 Feb 24 '17 at 9:16
  • Would just: select-string file.txt -pattern "." be of any use? – HelpingHand Feb 24 '17 at 9:31
  • 1
    cat .\food.txt | %{ "$($_.ReadCount) $_" } – JosefZ Feb 24 '17 at 10:03
  • 1
    cat .\food.txt|%{ "{0,4} {1}" -f $_.ReadCount,$_ } building on @JosefZ but numbers right justified – LotPings Feb 24 '17 at 12:53
  • Probably @LotPings is the shortest and most straightforward. – Bill_Stewart Feb 27 '17 at 16:52
1

I wrapped my line into a function you may include in one of your Powershell profiles.

Function nl 
{
<# .Synopsis
    Mimic Unic / Linux tool nl number lines
   .Description
    Print file content with numbered lines no original nl options supported
   .Example
     nl .\food.txt
#>
  param (
    [parameter(mandatory=$true, Position=0)][String]$FileName
  )

  process {
    If (Test-Path $FileName){
      Get-Content $FileName | ForEach{ "{0,5} {1}" -f $_.ReadCount,$_ }
    }
  }
}

Sample output:

> nl .\food.txt
    1 Apples
    2 Bananas
    3 Carrots
  • That is a nice solution. Thanks. Is there a (simple) way that could be adapted to accept pipeline input? (This is what I really want.) – StackzOfZtuff Nov 27 '17 at 9:52
  • @StackzOfZtuff checkout my answer – Keltari Nov 27 '17 at 11:20
1

This will do it and will work with any command that produces output:

$i = 1; cat food.txt | % {$i++;"$($i-1) `t $_"}

Here is the output:

1        Apples
2        Bananas
3        Carrots

Here is an example if with a directory listing:

$i = 1; dir | % {$i++;"$($i-1) `t $_"}

Here is the output:

1        backgrounds
2        boot
3        inetpub
4        PerfLogs
5        Program Files
6        Program Files (x86)
7        Riot Games
8        Users
9        Windows
10       Reflect_Install.log

Obviously, if you want line numbering to start at 0, then set $i = 0.

  • Or do $i=1; dir | % {"$($i) `t $_"; $i++}, rather than incrementing the variable before you want to, and then subtracting one from it. – Scott Mar 13 '19 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.