Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print/append row index number on each line in a file in Powershell

Tags:

powershell

sapmple.txt as below

row1col1||col2||col2||col3
row2col1||col2||col2||col3
row3col1||col2||col2||col3

expected

0||row1col1||col2||col2||col3
1||row2col1||col2||col2||col3
2||row3col1||col2||col2||col3

I coded like

get-content "C:\sample.txt" | foreach { "[|][|]" + $_ } | foreach { Index  + $_ } | set-content "C:\sample1.txt"

calling the pipe and then respective index, but not working.

Can you please help.

like image 766
user10118202 Avatar asked Oct 30 '25 00:10

user10118202


1 Answers

Another thought:

% { $i = 0 } { "$i||$_" ; $i++ }
like image 130
Bob Avatar answered Nov 01 '25 13:11

Bob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!