// remove line1 and line2 from file once
// (if line1 appears several times, only the first occurrence is removed, same for line2)
remove line1 and line2 from file once
(if line1 appears several times, only the first occurrence is removed, same for line2)
// remove line1 and line2 from file once // ( if line1 appears several times only the first occurrence is removed same for line2 )
func RemoveOnceFromFile(path string, line1, line2 string) (err error) {
	file, err := os.Open(path)
	if err != nil {
		return
	}
	defer file.Close()
	var bs []byte
	buf := bytes.NewBuffer(bs)
	scanner := bufio.NewScanner(file)
	line1RemovedOnce := false
	line2RemovedOnce := false
	for scanner.Scan() {
		currentLine := scanner.Text()
		if currentLine == line1 && !line1RemovedOnce {
			line1RemovedOnce = true
		} else if currentLine == line2 && !line2RemovedOnce {
			line2RemovedOnce = true
		} else {
			_, err = buf.WriteString(scanner.Text() + "\n")
			if err != nil {
				return
			}
		}
	}
	if err = scanner.Err(); err != nil {
		return
	}
	err = os.WriteFile(path, buf.Bytes(), 0644)
	return
}
func RemoveOnceFromFile ( path string , line1 , line2 string ) ( err error ) { file , err := os . Open ( path ) if err != nil { return } defer file . Close ( ) var bs [ ] byte buf := bytes . NewBuffer ( bs ) scanner := bufio . NewScanner ( file ) line1RemovedOnce := false line2RemovedOnce := false for scanner . Scan ( ) { currentLine := scanner . Text ( ) if currentLine == line1 && ! line1RemovedOnce { line1RemovedOnce = true } else if currentLine == line2 && ! line2RemovedOnce { line2RemovedOnce = true } else { _ , err = buf . WriteString ( scanner . Text ( ) + " \n " ) if err != nil { return } } } if err = scanner . Err ( ) ; err != nil { return } err = os . WriteFile ( path , buf . Bytes ( ) , 0644 ) return }
remove line1 and line2 from file once
(if line1 appears several times, only the first occurrence is removed, same for line2)
remove line1 and line2 from file once ( if line1 appears several times only the first occurrence is removed same for line2 )