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
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

-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
Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

I just tried the command above and still does not seem to work.
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

How does this work for you?

$regex = [regex]"isa\s+\w"
$regex.Split((gc c:\temp\extract.txt))
Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

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)
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

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

Avatar of BSonPosh
BSonPosh
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

It seems to be putting everything in one file(file.txt) in one line. Do you have any extensions installed? Have any ideas?
SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

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.
Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

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
Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

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

Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

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

Avatar of SasDev
SasDev
Flag of United States of America image

ASKER

Just needed [] added to go around the * to declare as a character. thank you for your Help!
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

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

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo