Solved

Fill an array based on an imported txt in file sharepoint

Posted on 2014-12-11
15
124 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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