Solved

Powershell to truncate string in text

Posted on 2016-08-05
6
199 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
[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
  • 3
  • 3
6 Comments
 
LVL 85

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 85

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
Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

 

Author Comment

by:motioneye
ID: 41744571
Hi oBdA
Yes I have tried that, still I have the same error as per screenshot
0
 
LVL 85

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

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.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

617 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