?
Solved

Extract a string from file

Posted on 2012-12-24
12
Medium Priority
?
1,410 Views
Last Modified: 2012-12-26
I need some pointers / example of a powershell script that will extract a string from a file based on the starting position of that string.

Appreciate the feedback.
0
Comment
Question by:Roberto Madro R.
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 38719402
You can use Select-String, for example following command will retrieve the lines strting with subsun from file test.txt
gc c:\test.txt | Select-String -Pattern "^subsun"
More for more examples you can refer the following articles. If you have any specific requirement then you can ask, so we can provide you the exact command to retrieve the required string..

References :
http://technet.microsoft.com/en-us/library/hh849903.aspx
http://www.computerperformance.co.uk/powershell/powershell_select_string.htm
0
 

Author Comment

by:Roberto Madro R.
ID: 38719405
Following "Pattern"s is fairly straightforward, my problem is, I don't have a pattern in the file, I however know the starting and endining position of the string I'm after and extracting it that way ensures accuracy.
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 38719411
You can try the "substring" command:

$Test = "Hello World"
$Test.substring(0,5)
Output: Hello

Or you can do a split based on characters like space or hyphen

$Test.split(" ")[0]
Output: Hello
$NewTest = $Test.split(" ")
$NewTest.count
Output:2
$NewTest
Output:
Hello
World

HTH,

DH
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Expert Comment

by:Subsun
ID: 38719427
If you want to extract based on the string position, then you can try to retrieve the line based on index and again use substring to get exact string value....

From following sample text file..
Subsun Test EE
Subsun Testing EE
Subsun EE Testing

Open in new window


Following command will return Testing from line 2
(gc test.txt | Select-Object -Index 1).substring(7,7)
Following command will return EE from line 3
(gc test.txt | Select-Object -Index 2).substring(7,2)

Not sure if this is what you are looking for..
0
 

Author Comment

by:Roberto Madro R.
ID: 38719435
We're getting close and I'll try your suggestion, the file I have in mind has only one line and it's around 400 characters, and I'm only interested in extracting any value between position 29 and position 40, sometimes the value between these two positions could be 5 digits (that's for older customers), and sometimes it could be 8,9, 10 digits (for newer customers), so as long as I can get the value (whatever it is) between position 29 and 40 I'm happy.

Thx
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38719438
For single line file, you don't have to add index..
(gc test.txt).substring(28,11) should retrieve the characters from 29 to 40..
0
 

Author Comment

by:Roberto Madro R.
ID: 38719451
Thank you Subsun, I think your suggestion will work, but I just examined one of the files I'm dealing with and found that the spaces between delimiters collapses in based on the number of characters, so if I was to use the solution you recommended, the optimal way would be to begin at position 29 but end at the delimiter, in my case it is a comma "," this way if my customer ID is 5 digits and it begins at positon 29, I'd only need to grab whatever between position 29 and 34 and since the customer ID varies the only way to ensure that I'm only getting the cusomter ID is to grab whatever value between position 29 and the next delimiter.  sorry I didn't explain it this way earlier.
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 38719483
Here's a solution I made a while back that does exactly what you're asking provided the line is always the same except for the customer ID

Using Subsun's previous information:

#We will first need to set the length of the whole string without the customer ID
$NormalLength = 142
#Then we get the length of the actual string
$Length = (gc test.txt).length
#Then we are going to do some simple math to figure out how large the customer ID is

#It's expressed like this: CustomerID Length = Length of string - Normal Length
$CustomerIDLength = $Length - $NormalLength

#Putting it all together in one line:

$CustomerID = (gc test.txt).substring(28,$((gc test.txt).length - 142))

or if you wanted to get the position 29, then split by "," (comma), then grab from the 29th position to the first comma, you could do something like this:

$CustomerID = ((gc test.txt).substring(28, 30)).split(",")[0]

I used 30 arbitrarily saying "the customer ID will never be longer than 30 characters".  You can change this accordingly.

Good luck!

-Dale
0
 

Author Comment

by:Roberto Madro R.
ID: 38719486
I'm getting this error now.

Method invocation failed because [System.Object[]] doesn't contain a method named 'substring'
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 38719498
Try adding a .tostring() in between your .substring and the closing parentheses.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 38719674
Try
((gc test.txt).substring(28)).split(",")[0]


For example..
PS C:\> gc test.txt # To see the content of file
ABCD EDFGHHIJKLMNABCD EDFGH START XXXXXXXXX XXXXXXXXXXXX XXX END ,NNN,NN,NN,NN
PS C:\> ((gc test.txt).substring(28)).split(",")[0]
START XXXXXXXXX XXXXXXXXXXXX XXX END

Open in new window

And if you are getting error [System.Object[]] doesn't contain a method named 'substring' then you must have more than one line in text file.. So you need to use -index 0 to get the first line..
((gc test.txt | Select-Object -Index 0).substring(28)).split(",")[0]
0
 

Author Closing Comment

by:Roberto Madro R.
ID: 38721560
Many thanks Subsun, right on the money, I appreciate the effort, it's the answer I've been looking for.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

770 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