Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - add an empty line to txt file every two rows.

Reply
 
Old 07-31-2008   #1 (permalink)
Member


Join Date: May 2008
Vista Home Premium 32bit
 
 

add an empty line to txt file every two rows.

Hi guys. I've a txt file like this

line1
line2
...

line n

and so on

I need to create a new file adding an empty line every two rows

line1
line2

line3
line4

line5

I wrote this code:

$i=0; gc myfile.txt | % {$i=$i++; if($i%2 -eq 0) {add-content newfile.txt $_} else {add-content newfile.txt `n,$_}}

but the file remains the same. Where's my mistake? Thanks in advance.

My System SpecsSystem Spec
Old 07-31-2008   #2 (permalink)
Member


Join Date: May 2008
Vista Home Premium 32bit
 
 

Re: add an empty line to txt file every two rows.

I spent an hour looking for a solution. Now that I posted finally I found it.

$i=1; gc myfile.txt | % {if($i%2 -ne 0) {add-content newfile.txt $_} else {add-content newfile.txt $_,`r};$i++}

Thanks the same. Have a nice day!
My System SpecsSystem Spec
Old 07-31-2008   #3 (permalink)
Kiron
Guest


 
 

Re: add an empty line to txt file every two rows.

Shorter version:
# insert NL every 2 lines
sc new.txt (gc old.txt | % {$l = 1} {if ($l++ % 2) {$_} else {$_,''}})

--
Kiron
My System SpecsSystem Spec
Old 08-01-2008   #4 (permalink)
Member


Join Date: May 2008
Vista Home Premium 32bit
 
 

Re: add an empty line to txt file every two rows.

Great solution Kiron! Thank you very much.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
find last non-empty line VB Script
How do I filter rows in one csv file matching a value in another PowerShell
file folder icons display non-empty folders as empty Vista file management
Count the number of Columns/rows in a CSV file.... PowerShell
Mysterious empty line in DateTime formatting PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46