Solved

Removing Duplicate words from lines in a power shell

Posted on 2015-02-23
5
93 Views
Last Modified: 2015-02-24
Hi

I am trying to find away to remove Duplicate words from a text file using Powershell

   
Name1 PC1234 PC1234 start Tue 2/17 6:53
Name2 PC5678 PC5678 start Tue 2/17 7:51

What I would like is


Name1 PC1234 start Tue 2/17 6:53
Name2 PC5678 start Tue 2/17 7:51

I have managed to get it working using the following but I would like to be able to open a .txt file remove the duplicate on all lines and right the content back to a new file.

$text= "Name1 PC1234 PC1234 start Tue 2/17 6:53"
$text.Length
$text.remove(14)

Open in new window


Thank you of your help
0
Comment
Question by:Si Walton
5 Comments
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 40626292
Might not be a solution that your looking for, but the easiest way to accomplish this is in Excel and using the remove duplicate button.

Will.
0
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 40626329
So the third column is always a copy of the second?
This will remove the third column:
Get-Content C:\Temp\in.txt | % {$_.Split(" ")[0..1+3..($_.Length -1)] -join " "} | Set-Content C:\Temp\out.txt

Open in new window

0
 
LVL 1

Author Comment

by:Si Walton
ID: 40627722
Hi oBda

Almost there and I understand the $_.Split

How would I be able to get the code to ignore the 1st line in the text file.


Keep this text
Name1 PC1234 PC1234 start Tue 2/17 6:53
Name2 PC5678 PC5678 start Tue 2/17 7:51

Thank you

Si
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 40627914
$(
Get-Content C:\Temp\in.txt | select -First 1
Get-Content C:\Temp\in.txt | select -Skip 1 | % {$_.Split(" ")[0..1+3..($_.Length -1)] -join " "}
) | Set-Content C:\Temp\out.txt

Open in new window

0
 
LVL 1

Author Closing Comment

by:Si Walton
ID: 40627936
Hi oBda and Qlemo

Thank you the solutions worked perfectly

Si
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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