使用Windows / DOS的shell /批处理命令,我怎么拿一个文件,并只保留独特的线路? [英] Using windows/dos shell/batch commands, how do I take a file and only keep unique lines?
问题描述
说我有一个像文件:
苹果
梨
柠檬
柠檬
梨
橙子
柠檬
我如何让这个我只保留了独特的线条,让我得到:
苹果
梨
柠檬
橙子
我可以修改的原始文件或创建一个新的。
我想有扫描原始文件在一个时间线,检查在新文件中是否存在该行,然后追加如果没有一种方式。我不处理这里真正的大文件。
关闭@echo
SETLOCAL disabledelayedexpansion
设置preV =
FOR / Fdelims =%% f由于('排序uniqinput.txt')做(
设置电流= %% F
SETLOCAL enabledelayedexpansion
如果!preV! NEQ!CURR!回音!CURR!
ENDLOCAL
设置preV = %% F
)
做些什么:首先排序输入,然后进入虽然依次输出只有当前行是previous不同。它可能是如果没有需要处理特殊字符更简单(这就是为什么那些 SETLOCAL / ENDLOCAL
是)。结果
它只是相呼应行标准输出
,如果你想写入文件做(假设你被点名批 myUniq.bat
) myUniq>> output.txt的
Say I have a file like:
apple
pear
lemon
lemon
pear
orange
lemon
How do I make it so that I only keep the unique lines, so I get:
apple
pear
lemon
orange
I can either modify the original file or create a new one.
I'm thinking there's a way to scan the original file a line at a time, check whether or not the line exists in the new file, and then append if it doesn't. I'm not dealing with really large files here.
@echo off
setlocal disabledelayedexpansion
set "prev="
for /f "delims=" %%F in ('sort uniqinput.txt') do (
set "curr=%%F"
setlocal enabledelayedexpansion
if !prev! neq !curr! echo !curr!
endlocal
set "prev=%%F"
)
What it does: sorts the input first, and then goes though it sequentially and outputs only if current line is different to previous one. It could have been even simpler if not for need to handle special characters (that's why those setlocal/endlocal
are for).
It just echoes lines to stdout
, if you want to write to file do (assuming you named your batch myUniq.bat
) myUniq >>output.txt
这篇关于使用Windows / DOS的shell /批处理命令,我怎么拿一个文件,并只保留独特的线路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!