Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Parse a text file, I'm lost

$
0
0

I admit I'm am new to powershell and i'm lost. I'm using powershell V 3 on Win 7.
What I really need is to process a directory of 50 to 100 text files with various names.

PS C:\> dir c:\data
    Directory: C:\data
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        11/28/2014  11:23 PM       1213 test.txt
-a---        11/29/2014   6:52 PM       1275 test1.txt
-a---        11/29/2014   9:11 PM       1073 test2.txt

This is one of the text files showing all the lines.

PS C:\> get-content c:\data\test2.txt
fileserver
hostname
11/22/2014 2:40 PM
Connecting to host fileserver, port 5201
Reverse mode, remote host fileserver is sending
[  4] local 192.168.169.138 port 1392 connected to 192.168.169.251 port 5201
[ ID] Interval           Transfer     Bandwidth
[  4]   0.00-1.01   sec  61.9 MBytes   513 Mbits/sec
[  4]   1.01-2.01   sec  52.2 MBytes   440 Mbits/sec
[  4]   2.01-3.01   sec  56.8 MBytes   478 Mbits/sec
[  4]   3.01-4.00   sec  56.6 MBytes   476 Mbits/sec
[  4]   4.00-5.00   sec  56.6 MBytes   476 Mbits/sec
[  4]   5.00-6.01   sec  53.8 MBytes   446 Mbits/sec
[  4]   6.01-7.01   sec  56.7 MBytes   477 Mbits/sec
[  4]   7.01-8.01   sec  57.1 MBytes   481 Mbits/sec
[  4]   8.01-9.00   sec  57.3 MBytes   482 Mbits/sec
[  4]   9.00-10.00  sec  58.4 MBytes   491 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth
[  4]   0.00-10.00  sec   568 MBytes   476 Mbits/sec                  sender
[  4]   0.00-10.00  sec   568 MBytes   476 Mbits/sec                  receiver

What I need to do is parse out some of the data in each file to a CSV file (one file as one line) for input into an Excel spreadsheet.
The very bad script below gets the data I need from one file but not in the format I need.

$all = get-content c:\data\test2.txt
$server = $all[0]
$hst = $all[1]
$tme = $all[2]
$jk = $all[19]
$jk1 = -split $jk
$bwu = $jk1[6]
$jk2 = $all[5]
$jk3 = -split $jk2
$lip = $jk3[3]
$rip = $jk3[8]
$jk4 = $all[20]
$jk5 = -split $all[20]
$bwd = $jk5[6]
$server, $rip, $hst, $lip, $tme, $bwu, $bwd
fileserver
192.168.169.251
hostname
192.168.169.138
11/22/2014 2:40 PM
476
476

What I really want is for each of the files to have one line in the output file.
fileserver,192.168.169.251,hostname,192.168.169.138,11/22/2014 2:40 PM,476,476

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles