Search pattern in directory and extract string from files using PowerShell

  • Thread starter Thread starter Rakesh M J
  • Start date Start date
R

Rakesh M J

Guest
I have almost 400 .sql files where i need to search for a specific pattern and output the results.

e.g

*file1.sql

select * from mydb.ops1_tbl from something1 <other n lines>

*file2.sql

select * from mydb.ops2_tbl from something2 <other n lines>

*file3.sql

select * from mydb.ops3_tbl ,mydb.ops4_tbl where a = b <other n lines>

Expected result


file1.sql mydb.ops1_tbl

file2.sql mydb.ops2_tbl

file3.sql mydb.ops3_tbl mydb.ops4_tbl

Below script in powershell - able to fetch the filename

Get-ChildItem -Recurse -Filter *.sql|Select-String -pattern "mydb."|group path|select name

Below script in powershell - able to fetch the line

Get-ChildItem -Recurse -Filter *.sql | Select-String -pattern "mydb." |select line

I need in the above format, someone has any pointers regarding this?


Rakesh Jayaram http://blogs.msdn.com/b/rakesh_ramblings/

Continue reading...
 
Back
Top