Beispiel #1
0
func fileReplace(item, searchStr, replaceStr string) {
	text, err := mcore.ReadFileAll(item)
	// cannot read file
	if nil != err {
		fmt.Printf("Read file error: %v\n", err)
		return
	}
	if !strings.Contains(text, searchStr) {
		return
	}
	nums := strings.Count(text, searchStr)
	fmt.Printf("File: %s found %d matches.\n", item, nums)
	//do replace
	text = strings.Replace(text, searchStr, replaceStr, -1)
	if _, err := mcore.WriteFile(item, text); err != nil {
		fmt.Printf("Write file %s \n Error: %v", item, err)
	} else {
		fmt.Println("Write file :", item)
	}
}
Beispiel #2
0
func Search(path string, exts string, recursive bool, skipDirs, skipFiles, content string) {
	files := mcore.GetSubFiles(path, recursive, exts, skipDirs, skipFiles)
	for _, item := range files {
		text, err := mcore.ReadFileAll(item)
		if nil != err {
			continue
		}

		if !strings.Contains(text, content) {
			if verbose {
				fmt.Printf("File: %s not found matches\n", item)
			}
			continue
		} else {
			nums := strings.Count(text, content)
			fmt.Printf("File: %s found %d matches.\n", item, nums)
		}

		//found
		data, err := mcore.ReadFileLines(item)

		if err != nil {
			fmt.Println(err)
			continue
		}

		for lineNum, line := range data {
			if strings.Contains(line, content) {
				fmt.Printf("%d ", lineNum+1)
				lineA := mcore.String(line).Split(content)
				for i, v := range lineA {
					fmt.Printf(v)
					if i != len(lineA)-1 {
						mcon.PrintGreen(content)
					}
				}
				fmt.Println()
			}
		}
	}
}