header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Removing lines from text with matching strings
Last Post 16 Mar 2010 06:48 PM by cameronove. 20 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:18 PM  
I just started scripting in PS yesterday so I'm as new as they come. I usually script in bash but have the need for a PS script for a test I'm conducting. I am opening an xls file from a remote share, and timing the command using measure-command. Here is what the output looks like (which I have going to a text file using out-file along with a time stamp using date);

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 22
Milliseconds      : 39
Ticks             : 220398230
TotalDays         : 0.000255090543981481
TotalHours        : 0.00612217305555556
TotalMinutes      : 0.367330383333333
TotalSeconds      : 22.039823
TotalMilliseconds : 22039.823

All I need is the bold part, the "TotalSeconds" line, the rest I want to remove. The text file does have a time stamp going in using date. In bash I use sed to find a matching string like "Days" and remove the line which for this example would remove 2 lines; "Days" and "TotalDays." Any ideas? I'm going to run this script every 5min for a few days and I don't want a bloated text file.




derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:22 PM  
I realize that I can cat this file and pipe out results I want, and redirect the results to a new file, was just wondering if there is a more elegant solution.
EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 06:27 PM  
Could you show the code that you are using to generate that output? I suspect that what you are actually generating is an object with all those properties. So to get what you want, all you would have to do is select just the one property that you want.
"Look Ma...no strings!"
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:30 PM  
date | out-file -filepath "D:\test\results.txt" -append

measure-command { \\[server]\[share]\[file.xls] } | out-file -filepath "D:\test\results.txt" -append
EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 06:32 PM  
Try changing:

measure-command { \\[server]\[share]\[file.xls] } | out-file -filepath "D:\test\results.txt"

to:

measure-command { \\[server]\[share]\[file.xls] } | Select-Object TotalSeconds | out-file -filepath "D:\test\results.txt"
"Look Ma...no strings!"
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:33 PM  
Ok I'll try it thanks
EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 06:38 PM  
It sounds like you are quite familiar with existing shells. The biggest AHA! moment I predict you will have (it was the biggest for me anyway) is the realization that in PS, everything is an object and should be treated as such. Just look at my signature on this forum. :)
"Look Ma...no strings!"
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:38 PM  
That didn't quite work but I think I;

| select-object TotalSeconds >> "D:\test\results.txt"

might.
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:42 PM  
Oh my bad both your suggestion and my previous one worked, its just that the output now had like 10 tabs of white space in front. Here is the new output;

TotalSeconds
------------
17.184737

I pasted in the result but it doesn't quite do the real spacing justice - in the text doc I had to scroll right to see them. Thanks for the help and quick responses appreciate it.

derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:52 PM  
I guess now what I ought to do is put my results in a spreadsheet (date in one column and results in another) so I save myself the work later...
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 06:59 PM  
Alright theres too much white space with the select-object, I'll just cat and "grep" out what I need and redirect that to a new file. Thanks for the help though much appreciated.
EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 07:10 PM  
measure-command { \\[server]\[share]\[file.xls] } | Select-Object TotalSeconds | Foreach-Object{$_.Trim()} | out-file -filepath "D:\test\results.txt"

Remember, everything is an object. String objects already know how to remove white space.
"Look Ma...no strings!"
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 07:24 PM  
Thanks again. Now I need to choose how to run it every 5 minutes. Should I use task scheduler or the entire script in a loop? What you think?
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 07:25 PM  
I'm not very object oriented (pun kind of intended) so I guess I'm not too familiar with the differences.
Cruisader03User is Offline
Basic Member
Basic Member
Posts:243
Avatar

--
11 Mar 2010 07:27 PM  
Try this one on for size:

(measure-command { \\[server]\[share]\[file.xls] }).TotalSeconds | Out-File D:\test\results.txt

or in CSV:

measure-command { \\[server]\[share]\[file.xls] } | Select TotalSeconds | Export-CSV D:\test\results.csv -NoTypeInformation
When at first you don't succeed Step-Into

http://theposherlife.blogspot.com
http://www.jandctravels.com

Cruisader03User is Offline
Basic Member
Basic Member
Posts:243
Avatar

--
11 Mar 2010 07:29 PM  
Creating a scheduled task to run every 5 min could be your best option. What is the purpose? This may help determine the best way to run this.
When at first you don't succeed Step-Into

http://theposherlife.blogspot.com
http://www.jandctravels.com

EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 07:31 PM  
That is an interesting question. Will you be running it 24/7 or just a few hours a day? Personally I think I would resort to having the script at the end of every run schedule itself for the next run using the soon.exe application. Think that is still in one of the resource kits from MS. I would just use soon because I have it and I'm lazy. Otherwise, I would have it use schtasks.exe. It is more annoying than soon.exe but it should already be on any windows box. The real PS way to do it though would probably be through WMI, but again, like I said I'm lazy.

Even lazy though, if I foresaw needing to do this with scripts on a regular basis (if I was going to do a lot of monitoring scripts lets say), then it would be worth my effort to create a function that would emmulate the functionality of soon.exe but use WMI to do it. Then I would put that function in my $profile script.
"Look Ma...no strings!"
EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 07:32 PM  
Cruisader03's method of getting the property is better than mine. I just send things down the pipe out of habit. :)
"Look Ma...no strings!"
EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 07:32 PM  
Cruisader03's method of getting the property is better than mine. I just send things down the pipe out of habit. :)
"Look Ma...no strings!"
derelictUser is Offline
New Member
New Member
Posts:12
Avatar

--
11 Mar 2010 09:15 PM  
I support 2 offices, one in Colorado and one in London. Users in London say the shared drive is "slow" so I'm trying to quantify slow. We have a colocation (with better throughput) that I've setup a windows share on, and put a 500KiB xls on. My users are not tech savvy so they're just opening the file from the shared location (which takes longer than copying the file locally and then opening it). So my script is to run every 5min to give me a real-world representation of a users experience opening these files. Using the logged data I'm going to graph these load times (and potentially latency just to the file just before loading) and graph it - see if there are any trends in traffic that cause "slow" results. I'm trying to determine if our colocation is an adequate solution or if I need to look at other shared resource locations like paid cloud services.
cameronoveUser is Offline
Basic Member
Basic Member
Posts:224
Avatar

--
16 Mar 2010 06:48 PM  
The following script will do a 24 hour trend analysis testing every 5 minutes.  If you want more than 24 hours then remember there are 12 test that can be done every 5 minutes per hour.  Multiply 12 by the number of hours you want to test and change the 288 in the for loop to that number. 

Open a separate PoSH console and let it run until it finishes.  I used Cruisader's method because I liked it best, but you could use any other measuring method you like in the loop.  If all you are doing is quantifying the time it takes then the test need not be permanent.  A simple loop seemed the easiest way to me.
for($i=1;$i -le 288;$i++){            
measure-command { \\[server]\[share]\[file.xls] } | Select TotalSeconds | Export-CSV D:\test\results.csv -NoTypeInformation
Start-Sleep -Seconds 300}
Cameron
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer