0

I would like to search the string failed in a text file and if found copy the entire line to another file.

Contends of test.txt:

: \Hardware\Disk Enclosure 27              diskslot1 failed  
: \Hardware\Disk Enclosure 27              diskslot2 normal  
: \Hardware\Disk Enclosure 27              diskslot3 normal 
: \Hardware\Disk Enclosure 27              diskslot4 normal    

Please help on the script

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
user3524788
  • 11
  • 1
  • 2
  • 2
  • The `Select-String` cmdlet is made to do things like this. Run `Get-Help Select-String` and you'll read, among other things: "By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match." – Robert Westerlund Jun 02 '14 at 06:03

2 Answers2

6

Try this:

select-string -path "D:\Textfile.txt" -Pattern "Text" | select line | out-file d:\outputfile.txt -append

Edit for Output without Line

$Output = select-string -path "d:\textfile.txt" -pattern "Text". 
$Output.line | out-file d:\outputfile.txt -append
Daniel4711
  • 402
  • 10
  • 24
0

This script prints every line containing "myword":

f = open('file.txt')
for line in f:
       if "myword" in f:
              print (line)
else: print(a)

f.close()
Sameh Weangy
  • 55
  • 1
  • 9