Solved

Fill an array based on an imported txt in file sharepoint

Posted on 2014-12-11
15
117 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
  • 8
  • 5
  • 2
15 Comments
 
LVL 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
output:

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

file attached
example-input.txt
0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
Comment Utility
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
 

Author Comment

by:Hans de Jongh
Comment Utility
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
Comment Utility
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 18

Expert Comment

by:Raheman M. Abdul
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
Comment Utility
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
Comment Utility
ehu im using windows 8.1 / 2k12r2
0
 

Author Comment

by:Hans de Jongh
Comment Utility
tried the new code aswell but same problem..
0
 
LVL 18

Expert Comment

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

Author Comment

by:Hans de Jongh
Comment Utility
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 18

Expert Comment

by:Raheman M. Abdul
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
thanks footech!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Set OWA language and time zone in Exchange for individuals, all users or per database.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to dynamically set the form action using jQuery.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now