How can I use Powershell to remove dupliation within a file?

Posted on 2014-09-23
Last Modified: 2014-09-24
I am currently working with this script.

I need to convert this script to a Powershell script.
It looks within a file for duplication, and if there is duplication it deletes the duplication.


REM Defile file location
set File1=c:\Programs\New.txt
set Workfile=c:\Programs\Temp\_workfile_.txt

REM Error if file does not exist
if not exist "%File1%" (
  goto :next_Script

REM Remove duplicate lines from the file
  copy NUL "%Workfile%" >NUL
  for /f "tokens=* usebackq" %%B in ("%File1%") do (
    findstr /b /e /c:"%%B" /i "%Workfile%">NUL || echo.%%B>>"%Workfile%"
  copy /y "%Workfile%" "%File1%" >NUL
  if exist "%Workfile%" del "%Workfile%"
Question by:100questions
  • 5
  • 3
LVL 29

Accepted Solution

becraig earned 500 total points
ID: 40340147
easy one liner
gc .\current-file.txt | sort -unique | out-file newfile.txt

Open in new window

LVL 29

Expert Comment

ID: 40340209
Are you doing this for more than one file ?

Author Comment

ID: 40341599
Actually, I am amalgamating files together, combining that is.. copy *.txt newfile.txt and then I need to remove any duplication within the file itself.  
It needs to check any duplication line by line, comparing lines to lines etc..

Author Comment

ID: 40341631
The script does not work as expected, it's reorganizing all the data into alpha order.  Perhaps I should have made this clearer.  
It need to look at every line and remove any lines which are duplicates, respecting the order of the data which currently exists.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

ID: 40341736
I have posted another question which is much clearer.

Author Closing Comment

ID: 40341975
This removes duplication of lines, however in the context of what I am looking for it needs to keep one set of data, between a start marker and an end marker as it were.  This does not respect that structure.  I have opened another question which is much more detailed and cleared.
LVL 29

Expert Comment

ID: 40342122
Great, I will look out for it.

I should be able to make a minor modification and get you to do exactly what you need without changing the sorting of the data.

Author Comment

ID: 40342147
Thanks for your help.

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

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.

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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

24 Experts available now in Live!

Get 1:1 Help Now