Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fill an array based on an imported txt in file sharepoint

Posted on 2014-12-11
15
Medium Priority
?
128 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 41

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 2000 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 41

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

618 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