You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now Columns takes an int, and uses strings.Fields to split the line. I'd like to propose we add a delimiter, so that you could parse csv, semicolon, or pipe delimited files. The function would look like this:
func (p *Pipe) Column(col int, d ...string) *Pipe {
var delim string
if len(d) > 0 {
delim = d[0]
}
return p.FilterScan(func(line string, w io.Writer) {
var columns []string
if delim == "" {
columns = strings.Fields(line)
} else {
columns = strings.Split(line, delim)
}
if col > 0 && col <= len(columns) {
fmt.Fprintln(w, columns[col-1])
}
})
}
d is a ...string means it takes 0 or more arguments, so if the user doesn't pass anything it still works maintaining compatibility with existing programs
Ex:
script.Exec(`echo "one two three"`).Columns(2).Stdout()
Would also print 2
Sort of a simple change, and it works for a case where I want to parse through comma separated lines. While I think it works, I might not be thinking of a use case where it would break existing code.
The text was updated successfully, but these errors were encountered:
Right now Columns takes an int, and uses strings.Fields to split the line. I'd like to propose we add a delimiter, so that you could parse csv, semicolon, or pipe delimited files. The function would look like this:
d is a ...string means it takes 0 or more arguments, so if the user doesn't pass anything it still works maintaining compatibility with existing programs
Ex:
would still print 2. However
Would also print 2
Sort of a simple change, and it works for a case where I want to parse through comma separated lines. While I think it works, I might not be thinking of a use case where it would break existing code.
The text was updated successfully, but these errors were encountered: