kctElgin
 New Member Posts:24

 |
| 20 Jun 2008 06:26 PM |
|
I am stuck again. I am trying to go through a directory of procs, dumping out the table names used.
$procnames = dir "*.prc" -name
foreach ($procname in $procnames) {
get-content $procname |
where-object {$_ -match "_tbl"} |
}
Now I've got just the lines where a table is probably referenced. Now I want to split the line into words, grab just the _tbl words and maybe count the number of times found so I can tell which tables to start with.
So how do I get this into a variable? $tblRef = foreach gets me an unexpected token "'in'" in expression or statement. Plugging it in before get-content works, as long as I want only the last procs information. Adding | out-file tablenames.txt captures only the last file processed. Trying to grab the output using "} > tablefile.txt " fails as well.
Substituting foreach-object in place of foreach resulted in no change. I had thought I'd understood Kirk Munro's blog on foreach the last time I read it, today I felt I was slogging through mud.
How do you capture or redirect the output that is scrolling by? |
|
|
|
|
halr9000 PowerShell MVP, Site Admin
 Basic Member Posts:335

 |
|
glnsize
 Basic Member Posts:101
 |
| 22 Jun 2008 08:56 PM |
|
Let the pipeline do the work for you!! The below one liner will return every line that contains _TBL. Where you go from there is up to you... Just remember that your not getting text!!!! Your getting an object back, so you can do anything with it. Export-cvs, Custom Object, out-file the options are endless. Just Don't think of it as text flying across the screen. :)
If you posted a small sample of the files your parsing I could try to be more help.
dir "*.prc" | %{ Get-Content $_.FullName | ?{$_ -match "_tbl"}}
spelled out
Get-ChildItem "*.prc" | ForEach-Object { Get-Content $_.FullName | Where-Object { $_ -match "_tbl"}}
-Glenn |
|
|
|
|
kctElgin
 New Member Posts:24

 |
| 24 Jun 2008 02:52 PM |
|
The parsing seems to work fine, it was getting the objects flying by into a variable I couldn't manage.
If I set it up as $NewFile = dir *.prc | foreach.... I'd get an error pointing at the word In, if I added the "| $newfile" within the foreach {} I'd end up with only the output from the last input file, or once I got the true/false result of the compare. Yet, the expected output scrolled down the screen. So I know I have a syntax problem, but I can't see it,even with the reading glasses on.
|
Attachment: 162453232871.txt
|
|
|
|
kctElgin
 New Member Posts:24

 |
| 24 Jun 2008 02:56 PM |
|
The parsing seems to work fine, it was getting the objects flying by into a variable I couldn't manage.
If I set it up as $NewFile = dir *.prc | foreach.... I'd get an error pointing at the word In, if I added the "| $newfile" within the foreach {} I'd end up with only the output from the last input file, or once I got the true/false result of the compare. Yet, the expected output scrolled down the screen. So I know I have a syntax problem, but I can't see it,even with the reading glasses on.
|
Attachment: 1624573532871.txt
|
|
|
|
halr9000 PowerShell MVP, Site Admin
 Basic Member Posts:335

 |
| 24 Jun 2008 03:37 PM |
|
Ugh, what is the deal with the forum! I swear, we are working on it. For some reason, some posts are getting canned... Here is the latest: RE: error with foreach/foreach-object by kctElgin The parsing seems to work fine, it was getting the objects flying by into a variable I couldn't manage. If I set it up as $NewFile = dir *.prc | foreach.... I'd get an error pointing at the word In, if I added the "| $newfile" within the foreach {} I'd end up with only the output from the last input file, or once I got the true/false result of the compare. Yet, the expected output scrolled down the screen. So I know I have a syntax problem, but I can't see it,even with the reading glasses on. |
|
Community Director, PowerShellCommunity.org Co-host, PowerScripting Podcast Author, TechProsaic |
|
|
kctElgin
 New Member Posts:24

 |
| 24 Jun 2008 04:29 PM |
|
A long time ago I heard "to a man with a hammer every problem looks like a nail". Still true and obviously I need to expand the number of tools I'm using.
This ends up not only being short, but allowing me to save the output to a variable.
And then there is the next opportunity. I saved the output to $tblName2
$tblName2 | %{$_.line} | Select-String "^\s*--\.*"
Returns all the things, commented lines, I want to exclude. |
|
|
|
|
kctElgin
 New Member Posts:24

 |
| 24 Jun 2008 07:36 PM |
|
Select-String has a -notmatch parameter that I couldn't get to work I plugged it in after the string and get the message the parameter does not exist.
$tblName2 | %{(($_.line).trim())} | Select-String -pattern "^\s*[^--]\.*"
This worked, but I had to add the trim to get rid of whitespace. Comments that began indented were not being deleted. Shouldn't "^\s*" say to ignore 0 or more occurences of tabs and spaces and find a leading --?
. And to go back to the original question on this thread, what is the difference between foreach and foreach-object? When I tried to assign a value to a variable using foreach-object ($procname in $procs){} PS gave me a syntax error. Eliminating the (line in group) and the foreach-object for the dir | foreach {} works as expected. I'd really like to understand, rather than try one and then switch if it doesn't work. [please see foreach thread --hal]
|
|
|
|
|
glnsize
 Basic Member Posts:101
 |
| 28 Jun 2008 10:23 PM |
|
How about something like this...
$out = @()
dir "*.prc" |%{ $current=$_.fullname ; Get-Content $_.FullName |%{ $_.trim()|% {$_.split(" ") }|? { $_ -match "_tbl"}}| %{$out += $_ | select @{e={$_};n='tbl'},@{e={$current};n='file'}}}
Then to drill down just use group-object.
$out | group-object tbl
you get the Idea, sorry i took so long to respond. Thank you for posting that file this one was actually kinda fun! Good grief I love this tool... Do you know how many lines of perl or VBscript it would taken to do the same thing!!!!!
-Glenn
|
|
|
|
|
kctElgin
 New Member Posts:24

 |
| 01 Jul 2008 04:18 PM |
|
Thanks for the help, from everyone.
By the time I got most of the script working I figured out it wasn't going to do anything constructive for what I had to get done. On the other hand, I've used some of the techniques and gotten a little smarter having gone through this.
kct
Firewall caught me when I originally tried to post and by the time I'd got past firewall, post was gone to the bit bucket. |
|
|
|
|