私は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
あなたはすべてのファイルとディレクトリにこのプログラムを実行する場合、それは重複を削除します。
あなたのニーズに合っていますか?
出典
2017-12-01 15:19:53
Noy
ここに来る前に、おそらく研究をしておくべきでしょう。これは契約の仕事を求める場所ではありません。 –
あなたは何をしたいのか作成するスクリプトを購入しようとしていますか?もしそうなら、私たちにあなたが使っている言語を知らせて、それを手伝ってくれるいくつかのコードを投稿してください。 – EasyE