Question : powershell HELP!! Reading file and separating text to seperate files

I've been banging my head against the wall lately your help is greatly appreciated. I'm pretty new to powershell so chances are I don't have the methods/correct logic.

All i want to do is remove line feeds and carriage returns
It seems to be simple to add in line feeds with `r `n with the -replace cmd...
(gc c:\extract.txt) -replace "~",''`r`n" | sc c:\extract.txt - works
but
(gc c:\extract.txt) -replace "`r`n",'' " | sc c:\extract.txt - does not work

the end result I want to open a file find text "isa" copy isa and all text after it until it finds isa again and write each isa and text to a separate file for each isa it finds.

example

original file(c:\text.txt)
isa textline1
textline2
textline3
isa textline1
textline2
textline3
and etc...

end result

file1(c:\extract1.txt)
isa textline1
textline2
textline3

file2(c:\extract2.txt)
isa textline1
textline2
textline3

I've tried a couple of different ways but nothing seems to be working

here's the code!

$now=get-Date
$yr=$now.Year.ToString()
$mo=$now.Month.ToString()
$dy=$now.Day.ToString()
$hr=$now.Hour.ToString()
$mi=$now.Minute.ToString()
      if ($mo.length -lt 2) {
            $mo="0"+$mo #pad single digit months with leading zero
            }
      if ($dy.length -lt 2) {
            $dy="0"+$dy #pad single digit day with leading zero
            }
      if ($hr.length -lt 2) {
            $hr="0"+$hr #pad single digit hour with leading zero
            }
      if ($mi.length -lt 2) {
            $mi="0"+$mi #pad single digit minute with leading zero
      }
$currenttime = ($yr + $mo + $dy + $hr + $mi)

function writefile([string]$txt) {
      #echo "blah"
      sleep -Seconds 1
      $index = $index + 1
      $textfile = "c:\$currenttime $index extract.txt"

      Add-Content $textfile $txt

}
$b = gc c:\test.txt | ForEach-Object{$_ -replace "`r`n",""}
$b.Split( "ISA" )
writefile $b

Answer : powershell HELP!! Reading file and separating text to seperate files

Try this. It worked for me
1:
2:
3:
4:
5:
6:
$i = 1
switch -regex -file "c:\temp\extract.txt"
{
   "^isa\s"   {if($fileString){$filestring | out-file "File$i.txt"};$fileString = $_;$i++}
   "^(?!isa)" {$fileString += "`r`n$_"}
}
Open in New Window Select All
Random Solutions  
 
programming4us programming4us