Solved

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

Posted on 2014-09-23
8
122 Views
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.


setlocal

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%"
)
0
Comment
Question by:100questions
[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
  • 5
  • 3
8 Comments
 
LVL 29

Accepted Solution

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

Open in new window

0
 
LVL 29

Expert Comment

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

Author Comment

by:100questions
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..
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

Author Comment

by:100questions
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.
0
 

Author Comment

by:100questions
ID: 40341736
I have posted another question which is much clearer.
0
 

Author Closing Comment

by:100questions
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.
0
 
LVL 29

Expert Comment

by:becraig
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.
0
 

Author Comment

by:100questions
ID: 40342147
Thanks for your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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.
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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