Text 删除文本文件中的重复行
我已经搜索过了,但无法获得一些执行以下总体任务的自动脚本: 1) 浏览文件夹中的所有文本文件 2) 从文本文件中删除重复的行/行(文本已排序,因此可以跳过排序部分) 3) 保存并覆盖文本文件 不幸的是,我搜索的所有结果只是为了从一个特定文件中删除一行,并另存为另一个文件名 然后我将设置一个调度任务来运行此脚本 我没有任何脚本知识,只有很少的批量脚本设置经验。非常感谢您的帮助和指导 不幸的是,我搜索的所有结果只是为了从一个特定文件中删除一行,并另存为另一个文件名 我想你的答案就在这里。我不知道你在用哪种语言写,但通常在这种情况下,我会这样做Text 删除文本文件中的重复行,text,duplicates,line,repeat,Text,Duplicates,Line,Repeat,我已经搜索过了,但无法获得一些执行以下总体任务的自动脚本: 1) 浏览文件夹中的所有文本文件 2) 从文本文件中删除重复的行/行(文本已排序,因此可以跳过排序部分) 3) 保存并覆盖文本文件 不幸的是,我搜索的所有结果只是为了从一个特定文件中删除一行,并另存为另一个文件名 然后我将设置一个调度任务来运行此脚本 我没有任何脚本知识,只有很少的批量脚本设置经验。非常感谢您的帮助和指导 不幸的是,我搜索的所有结果只是为了从一个特定文件中删除一行,并另存为另一个文件名 我想你的答案就在这里。我不知道你在
但关键的一点是,只需删除原始文件,然后将新文件重命名为原始文件。我用GoLang为您编写并注释了一个小脚本,如果您知道如何运行它,它可能会对您有所帮助。如果没有,快速研究将帮助你
package main
import (
"io/ioutil"
"strings"
"log"
"os"
)
func main() {
// get all files in directory
files, err := ioutil.ReadDir(".")
// check error
if err != nil { log.Println(err) }
// go through all the files
for _, file := range files {
// check if it's a txt file (can change this)
if strings.HasSuffix(file.Name(), "txt") { // you can change this
// read the lines
line, _ := ioutil.ReadFile(file.Name())
// turn the byte slice into string format
strLine := string(line)
// split the lines by a space, can also change this
lines := strings.Split(strLine, " ")
// remove the duplicates from lines slice (from func we created)
RemoveDuplicates(&lines)
// get the actual file
f, err := os.OpenFile(file.Name(), os.O_APPEND|os.O_WRONLY, 0600)
// err check
if err != nil { log.Println(err) }
// delete old one
os.Remove(file.Name())
// create it again
os.Create(file.Name())
// go through your lines
for e := range lines {
// write to the file without the duplicates
f.Write([]byte(lines[e] +" ")) // added a space here, but you can change this
}
// close file
f.Close()
}
}
}
func RemoveDuplicates(lines *[]string) {
found := make(map[string]bool)
j := 0
for i, x := range *lines {
if !found[x] {
found[x] = true
(*lines)[j] = (*lines)[i]
j++
}
}
*lines = (*lines)[:j]
}
您的文件:hello hello yes no
返回结果:hello yes no
如果在包含所有文件的目录中运行此程序,它将删除重复的文件
希望它能满足您的需要。您可能应该在来这里之前做一些研究。这不是一个要求合同工作的地方。你是想购买一个你想要的脚本还是创建它?如果是这样,请让我们知道您使用的是什么语言,并为我们发布一些代码,以帮助您完成它。可能的重复来自不清楚的问题,我认为他希望首先递归遍历目录以查找文本文件。。。可能是。这是一个很好的观点,我显然认为每个人都知道如何编写循环语句,而我似乎错过了关键点。老实说,我不知道为什么我甚至试图回答这个问题。。。