Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

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

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
100questions
Asked:
100questions
  • 5
  • 3
1 Solution
 
becraigCommented:
easy one liner
gc .\current-file.txt | sort -unique | out-file newfile.txt

Open in new window

0
 
becraigCommented:
Are you doing this for more than one file ?
0
 
100questionsAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
100questionsAuthor Commented:
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
 
100questionsAuthor Commented:
I have posted another question which is much clearer.
0
 
100questionsAuthor Commented:
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
 
becraigCommented:
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
 
100questionsAuthor Commented:
Thanks for your help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now