Golang : How to remove certain lines from a file
Alright, here is a simple tutorial on how to remove certain lines of data from a file. There are times due to some unforeseen reasons, some files that got downloaded contain more data that what was needed, such as extra HTTP request header information at the beginning of it.
So, how does one go around deleting those unwanted information?
The solution to this problem is similar to copying a file to a new file except that the unwanted data are omitted out during the copying process. For the code example below, we are going to remove the unwanted data at the beginning of the file simply be ignoring those lines. Once a marker %startHERE
is detected, then only we start reading the rest of the data.
Here you go!
NOTE: For simplicity sake, we will use corruptFile constant to mimic a file. For your own production use, uncomment those lines to read from an actual file.
package main
import (
"fmt"
//"io/ioutil"
//"os"
"strings"
)
const corruptFile = `HTTP 2.0 OK
Content-Length :123456
%startHERE
some real data bytes
more bytes`
func main() {
// UNCOMMENT these lines to read in from an actual file instead.
//if len(os.Args) <= 1 {
// fmt.Printf("USAGE : %s <target_filename> \n", os.Args[0])
// os.Exit(0)
//}
//fileName := os.Args[1]
//fileBytes, err := ioutil.ReadFile(fileName)
//if err != nil {
// fmt.Println(err)
// os.Exit(1)
//}
//lines := strings.Split(string(fileBytes), "\n")
lines := strings.Split(string(corruptFile), "\n")
startPrint := false
for i, line := range lines {
// only print after %startHERE
if equal := strings.Index(line, "%startHERE"); equal > 0 {
startPrint = true
}
if startPrint {
fmt.Println(i, line)
// instead of printing to the screen, this is where
// you want to start writing to a new file a.k.a copying the file
// how it is done at https://www.socketloop.com/tutorials/golang-simple-file-scaning-and-remove-virus-example
}
}
}
Hope this helps and happy coding!
See also : Golang : Display a text file line by line with line number example
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.2k Golang : Get IP addresses of a domain name
+18.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+7.1k Golang : File system scanning
+6.8k Golang : Find the shortest line of text example
+38.8k Golang : How to read CSV file
+4.5k Unix/Linux : How to pipe/save output of a command to file?
+17.3k Golang : Find smallest number in array
+5.4k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+11.3k Golang : Change date format to yyyy-mm-dd
+7k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+17.7k Golang : How to log each HTTP request to your web server?
+9k Golang : How to check if a string with spaces in between is numeric?