Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell to truncate string in text

Posted on 2016-08-05
6
Medium Priority
?
269 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
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 

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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
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, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrā€¦

722 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