Solved

Fill an array based on an imported txt in file sharepoint

Posted on 2014-12-11
15
125 Views
Last Modified: 2014-12-12
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
0
Comment
Question by:Hans de Jongh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
15 Comments
 
LVL 40

Expert Comment

by:footech
ID: 40494028
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
 

Author Comment

by:Hans de Jongh
ID: 40494055
output:

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

file attached
example-input.txt
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40494119
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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Author Comment

by:Hans de Jongh
ID: 40494129
thanks im going to test it. I don't know how many line of tags there will be btw. Sometimes one sometimes 10
0
 

Author Comment

by:Hans de Jongh
ID: 40494159
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
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40494226
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
 

Author Comment

by:Hans de Jongh
ID: 40494246
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
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 40494493
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
 

Author Comment

by:Hans de Jongh
ID: 40494496
ehu im using windows 8.1 / 2k12r2
0
 

Author Comment

by:Hans de Jongh
ID: 40494499
tried the new code aswell but same problem..
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40494505
please post the error for the new code and the sampe file (if you wish)
0
 

Author Comment

by:Hans de Jongh
ID: 40494545
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
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40494597
replace OS with OS:   in the code (on 3rd line) and it should work
ie
$index2=0..($text.count - 1) | ? { $text[$_] -match "OS:" }
0
 
LVL 40

Expert Comment

by:footech
ID: 40495086
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
 

Author Comment

by:Hans de Jongh
ID: 40495616
thanks footech!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question