Solved

Powershell to truncate string in text

Posted on 2016-08-05
6
43 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 82

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
The ouput return me with null, its strange though to see teh output like this.

Capture.PNG
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:motioneye
Comment Utility
Hi oBdA
Yes I have tried that, still I have the same error as per screenshot
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This article will help you understand what HashTables are and how to use them in PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now