简体   繁体   中英

Delete multiple lines from a file

I have a file that contains several lines of text.

Example:

Line 1
Line 2
Line 3
Line 4
Line 1
Line 2
Line 5
Line 6
Line 1
Line 2

I want to remove the repetitions of Line 1 and Line 2. And I want to do it from a command line on Windows (either cmd or Powershell). Is there any simple way to achieve it...? I want my output as follows...

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6

And one more thing to mention the repeated lines are always the same but they are in large number.

You can use Select-Object -Unique in Powershell.

Select-Object

For example:

Get-Content test.txt | Select-Object -Unique > new.txt

would populate new.txt with the unique lines in test.txt .

uniq命令就是这样: httpuniq

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
SET "line="
FOR /f "delims=" %%a IN ('sort q22351713.txt') DO (
 IF "!line!" neq "%%a" ECHO(%%a
 set "line=%%a"
)
)>newfile.txt

GOTO :EOF

I used a file named q22351713.txt for my testing. Produces newfile.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM