Board logo

标题: [文本处理] 删除重复行的linux代码如何改成bat? [打印本页]

作者: 5872169    时间: 2023-9-6 15:02     标题: 删除重复行的linux代码如何改成bat?

sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' 这条命令如何改成bat,我对linux不熟悉,谢谢!
作者: Batcher    时间: 2023-9-6 16:21

回复 1# 5872169


建议说清楚你想用这个代码实现什么功能?把测试文本一起发出来。
如果需要上传文件,可以用阿里云盘或百度网盘。

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
作者: Five66    时间: 2023-9-6 22:49

意思是用bat实现那条sed命令的功能么?
是的话建议你下载一个windows下能用的sed
作者: wanghan519    时间: 2023-9-7 09:52

复杂啊,真不如下个busybox-w32可以执行sed就好
  1. https://frippery.org/busybox/
复制代码

作者: Batcher    时间: 2023-9-7 16:03

回复 3# Five66


这个命令的作用是删除重复行,出自于大名鼎鼎的sed1line
http://www.pement.org/sed/sed1line.txt

中文版的解释
http://bbs.chinaunix.net/thread-306867-3-1.html#pid2031834

英文版的解释
https://catonmat.net/sed-one-liners-explained-part-three

使用[[:print:]]取代[ -~]可以解决某些环境下未能删除重复行的问题
https://stackoverflow.com/questions/5930246/what-does-this-sed-expression-from-todo-sh-do
Linux:
  1. sed -n 'G; s/\n/&&/; /^\([[:print:]]*\n\).*\n\1/d; s/\n//; h; P' 1.txt
复制代码
Windows:
  1. sed -n "G; s/\n/&&/; /^\([[:print:]]*\n\).*\n\1/d; s/\n//; h; P" 1.txt
复制代码
首先搞清楚几个基本命令的作用:
G Append hold space to pattern space.
d Delete pattern space. Start next cycle.
h Copy pattern space to hold space.
P Print up to the first embedded newline of the current pattern space.

然后使用 sedsed 执行一下观察详细过程:


批处理BAT脚本如何删除文本文件中重复的行
http://bbs.bathome.net/thread-5219-1-1.html
http://bbs.bathome.net/thread-5509-1-1.html
http://bbs.bathome.net/thread-4690-1-1.html
http://bbs.bathome.net/thread-5326-1-1.html
http://bbs.bathome.net/thread-4896-1-1.html




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2