Fill an array based on an imported txt in file sharepoint

Hello,

I have a text file with a lot of different information in it.
I want to import service tags (out of an invoice which I converted to txt).

now 2 thinks I know for sure:

1) the servicetags are after "Tag nrs" and before "PS: Wanneer u"
Tag nrs:             FFRH642 9KRH642 7GRH642 CGRH642 1HRH642

                     3PRH642

OS: Wanneer u
2) a servicetag is always 7 chars long.


In the array I need to keep the formatting.
So what is on a separate line needs to stay on a separate line..

I have no idea where to start (due to the multiple lines).

Regards

Hans
Hans de JonghAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
The best way forward is if you can provide a sample input file with several lines, and a sample output file showing exactly what you need the output to be based on the sample input file.
0
Hans de JonghAuthor Commented:
output:

PS D:\> $servicetags
FFRH642 9KRH642 7GRH642 CGRH642 1HRH642
3PRH642
PS D:\>

file attached
example-input.txt
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
I got this for you at the moment (may look weird but works)
$text = @(get-content C:\temp\servicetag.txt)
$index = 0..($text.count - 1) | ? { $text[$_] -match "tag" }
$text = $text[$index..($index+2)] | ? { $_ -ireplace "Tag nrs:","" }
($text[0].Trim()+" "+$text[1].Trim()).Replace("Tag nrs:","").trim()

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Hans de JonghAuthor Commented:
thanks im going to test it. I don't know how many line of tags there will be btw. Sometimes one sometimes 10
0
Hans de JonghAuthor Commented:
ok, I have tested it and it works but I tried with another example and it doesn't take extra lines.
For example I tested on an invoice with 6 lines of tags and it just takes the first 2 lines
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Modified to include more lines:
$text = @(get-content C:\temp\servicetag.txt)
$index = 0..($text.count - 1) | ? { $text[$_] -match "tag" }
$index2=0..($text.count - 1) | ? { $text[$_] -match "OS" }

$text = $text[$index..($index2-1)] | ? { $_ -ireplace "Tag nrs:","" }

($text[0..$text.length].Trim()).Replace("Tag nrs:","").trim()

Open in new window

0
Hans de JonghAuthor Commented:
PS P:\> $text = @(get-content C:\temp\servicetag.txt)
PS P:\> $index = 0..($text.count - 1) | ? { $text[$_] -match "tag" }
PS P:\> $index2=0..($text.count - 1) | ? { $text[$_] -match "OS" }
PS P:\> $text = $text[$index..($index2-1)] | ? { $_ -ireplace "Tag nrs:","" }
Method invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.
At line:1 char:1
+ $text = $text[$index..($index2-1)] | ? { $_ -ireplace "Tag nrs:","" }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Subtraction:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

PS P:\> ($text[0..$text.length].Trim()).Replace("Tag nrs:","").trim()$text = @(get-content C:\temp\servicetag.txt)
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
What version of powershell you are using?
Try this:
$text = @(get-content C:\temp\servicetag.txt)
$index = 0..($text.count - 1) | ? { $text[$_] -match "tag" }
$index2=0..($text.count - 1) | ? { $text[$_] -match "OS" }
$index2=$index2-1
$text = $text[$index..($index2)] | ? { $_ -ireplace "Tag nrs:","" }

($text[0..$text.length].Trim()).Replace("Tag nrs:","").trim()
                                          

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hans de JonghAuthor Commented:
ehu im using windows 8.1 / 2k12r2
0
Hans de JonghAuthor Commented:
tried the new code aswell but same problem..
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
please post the error for the new code and the sampe file (if you wish)
0
Hans de JonghAuthor Commented:
PS P:\> $text = @(get-content C:\temp\servicetags.txt)
PS P:\> $index = 0..($text.count - 1) | ? { $text[$_] -match "tag" }
PS P:\> $index2=0..($text.count - 1) | ? { $text[$_] -match "OS" }
PS P:\> $index2=$index2-1
Method invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.
At line:1 char:1
+ $index2=$index2-1
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Subtraction:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

PS P:\> $text = $text[$index..($index2)] | ? { $_ -ireplace "Tag nrs:","" }
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32".
At line:1 char:1
+ $text = $text[$index..($index2)] | ? { $_ -ireplace "Tag nrs:","" }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException

PS P:\>
PS P:\> ($text[0..$text.length].Trim()).Replace("Tag nrs:","").trim()
servicetags.txt
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
replace OS with OS:   in the code (on 3rd line) and it should work
ie
$index2=0..($text.count - 1) | ? { $text[$_] -match "OS:" }
0
footechCommented:
I know you've already accepted an answer here, but I just wanted to point out some code variations that would work.
# These first two give the same output as Raheman's
$data = Get-Content .\temp\example-input.txt | % {$_.Trim()} | ? {$_} | Out-String
If ( $data -match "Tag nrs: +(([A-Z0-9]{7}[\r\n ]+)+)OS:" )
{ $Matches[1] }

$data = Get-Content .\temp\example-input.txt | % {$_.Trim()} | ? {$_} | Out-String
[regex]::Matches($data,"(?s)Tag nrs: +(([A-Z0-9]{7}[\r\n ]+)+)OS:") | % {$_.Groups[1]} | % {$_.Value}

Open in new window


It seems to me that it's often not useful to keep the formatting from the input file, but instead it's desired to capture all the matching data to individual elements of an array.
#Here's a way of capturing all elements that match the pattern
$data = Get-Content .\temp\example-input.txt | % {$_.Trim()} | ? {$_} | Out-String
[regex]::Matches($data,"[A-Z0-9]{7}") | % {$_.Groups[0]} | % {$_.Value}

# Sometimes the pattern may appear in other parts of the file that you're not interested in,
# so you just want to look within a section of the file that matches another pattern.
$data = Get-Content .\temp\example-input.txt
$data | % -Begin {$capture = $false} -process `
{
    If ($_ -match "^Tag nrs:" )
    { $capture = $true }  #signal the beginning of the section to perform further matches against
    ElseIf ($_ -match "^OS:" )
    { $capture = $false }  #signal the end of the section
    If ( $capture -eq $true )
    { [regex]::Matches($_,"[A-Z0-9]{7}") | % {$_.Groups[0]} | % {$_.Value} }
}

Open in new window

0
Hans de JonghAuthor Commented:
thanks footech!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.