Avatar of SasDev
SasDevFlag for United States of America asked on

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
Powershell

Avatar of undefined
Last Comment
BSonPosh

8/22/2022 - Mon
BSonPosh

-replace is regular expression replace. You need to use the regex form of Carriage Return Line Feed

(gc c:\extract.txt) -replace "\r\n",'' " | sc c:\extract.txt
ASKER
SasDev

I just tried the command above and still does not seem to work.
BSonPosh

How does this work for you?

$regex = [regex]"isa\s+\w"
$regex.Split((gc c:\temp\extract.txt))
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
SasDev

Thank you! this works for getting a single line. how do you split text <isa & text>(into one file) then <isa & text>(into another file)
BSonPosh

I just doing this in my head so bear with me.

Try this
$File = gc c:\temp\extract.txt
$regex = [regex]"isa\s+"
$regex.Split($file) | %{ "{0}`n{1}" -f $matches[0],$_ }

Open in new window

BSonPosh

Just an FYI:

The -replace "\n"," " does work... the problem is that (gc filename) is returning an array of strings. So basically Powershell splits the file on new line and returns the array as the result.

I think what you need here is regex with a look ahead and label to extract the data you want.

Also.. you can use the JOIN method (this joins an array with a specific character) on the string class to create a String instead of an array

$data = gc c:\temp\extract.txt

So this
[system.string]::join("`n",$data) | sc c:\extract.txt

Would be the same as this (if it worked like you expected)
($data -replace "\n",'' ") | sc c:\extract.txt
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
BSonPosh

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
SasDev

It seems to be putting everything in one file(file.txt) in one line. Do you have any extensions installed? Have any ideas?
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
SasDev

I just ran your script on a new PC and it completes but doesn't create a file. so basically does nothing. i can echo $filestring and get the text. I'm not sure why it's not working.
ASKER
SasDev

Get this. I tried it on my windows 7 laptop. I get the following result. creates only 2 files called File2.txt and File3.txt. file2 contains the first record and file 3 contains the 2nd record. 3rd and 4th record are missing.

Let me know if you have any ideas.

What version of powershell are you using? the two desktops are powershell v2. and windows 7 is whatever it comes with
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
SasDev

I figured out why it's missing the 3rd and 4th record. the extract.txt file needs a space after each isa. i had
isa 1
isa 2
isa3
isa 4

now what its doing is making file1.txt as the 4th record. file2.txt as the 1st record and so on.

 So the real file will do a search on ISA*00*. it does not work because of the *. and if i do a search on ISA does not work as well. Have any ideas?
$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

ASKER
SasDev

I've figured out including * after some research and reading brackets are the answer looks messy but works.
$i = 1
switch -regex -file "c:\temp\extract.txt"
{
   "^ISA[*]00[*]\s"   {if($fileString){$filestring | out-file "File$i.txt"};$fileString = $_;$i++}
   "^(?!ISA[*]00[*])" {$fileString += "`r`n$_"}
}
 

Open in new window

ASKER
SasDev

Just needed [] added to go around the * to declare as a character. thank you for your Help!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
BSonPosh

glad you worked it out... without the file I would have never caught that.