Solved

Powershell to truncate string in text

Posted on 2016-08-05
6
86 Views
Last Modified: 2016-08-11
Guys,
I have a powershell script below use to truncate the string in the string_output2.txt however the script below doesn't work, what I would need to output is something like
this JJR3345-s-54007.maxis.kd.com save in string_output22.txt
Get-Content (Get-ChildItem F:\Powershell\string_output2.txt) | % {$_.Split(':')[1]} | Out-File F:\Powershell\string_output22.txt


Here is contain in string_output2.txt


JJR3345-bs-54007:dbxsinstnace:JJR3345-s-54007.maxis.kd.com
JJR3345-bs-54008:dbxsinstnace:JJR3345-s-54008.maxis.kd.com
JJR3345-bs-54009:dbxsinstnace:JJR3345-s-54009.maxis.kd.com
JJR3345-s-54025:dbxsinstnace:JJR3345-s-54025.maxis.kd.com
JJR3345-s-54001:dbxsinstnace:JJR3345-s-54001.maxis.kd.com
0
Comment
Question by:motioneye
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41743998
You don't need the "Get-ChildItem" since you're only addressing a single file, you need to use $_.Split(':')[2], and if you want to to overwrite the source file, you need to put Get-Content in parenthesis, so that PS will read the whole file before continuing with the pipeline.
(Get-Content F:\Powershell\string_output2.txt) | % {$_.Split(':')[2]} | Out-File F:\Powershell\string_output22.txt

Open in new window

0
 

Author Comment

by:motioneye
ID: 41744540
The ouput return me with null, its strange though to see teh output like this.

Capture.PNG
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41744548
That's a Unicode; use Out-File's -Encoding argument:
(Get-Content F:\Powershell\string_output2.txt) | % {$_.Split(':')[2]} | Out-File F:\Powershell\string_output22.txt -Encoding ASCII

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:motioneye
ID: 41744571
Hi oBdA
Yes I have tried that, still I have the same error as per screenshot
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41744702
Does
Get-Content F:\Powershell\string_output2.txt

Open in new window

print the expected strings?
Since you're replacing the original file, have you restored string_output2.txt to one that was created by the original source?
You can try Set-Content instead of Out-File as well.
0
 

Author Closing Comment

by:motioneye
ID: 41751635
Hi oBdA,
Thanks for help, I have to copy the text from one file and move to another new file, then the scripts worked with no flaw. thanks again for the assistance and the opinion
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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