Last active
January 5, 2024 21:24
-
-
Save ewollesen/3f54f1362b3521e0c0cf9288f33d3355 to your computer and use it in GitHub Desktop.
csvknife: a utility to cut a column from CSV data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// csvknife cuts a specified column from a CSV file. | |
// | |
// To build: | |
// | |
// go build . | |
// | |
// If you're reading this online, you can clone your own copy with a | |
// command like this: | |
// | |
// git clone https://gist.github.com/3f54f1362b3521e0c0cf9288f33d3355.git csvknife | |
// | |
// | |
// Copyright 2024 Eric Wollesen | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
import ( | |
"encoding/csv" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"strconv" | |
) | |
var ( | |
usageMsg = ` | |
The first argument is the 1-indexed column number of the CSV data to print. | |
Suggested shell example to cut column 3 (1-indexed) without a header (i.e. from Sumologic) into | |
jq for filtering: | |
$ %s --skip-header 3 < data.csv | jq --slurp | |
` | |
usageFn = func() { | |
var out = flag.CommandLine.Output() | |
var arg0 = os.Args[0] | |
fmt.Fprintf(out, "Usage of %s:\n", arg0) | |
flag.PrintDefaults() | |
fmt.Fprintf(out, usageMsg, arg0) | |
} | |
) | |
func main() { | |
var skipHeader = false | |
flag.BoolVar(&skipHeader, "skip-header", false, "when true, skip the first line") | |
flag.Usage = usageFn | |
flag.Parse() | |
colNum := func(arg string) int { | |
i, err := strconv.Atoi(arg) | |
if err != nil { | |
log.Fatal(fmt.Errorf("parsing column number: %w", err)) | |
} | |
return i | |
}(flag.Arg(0)) | |
records, err := csv.NewReader(os.Stdin).ReadAll() | |
if err != nil { | |
log.Fatal(fmt.Errorf("reading CSV data: %w", err)) | |
} | |
for idx, record := range records { | |
if idx == 0 && skipHeader { | |
continue | |
} | |
fmt.Println(record[colNum-1]) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module csvknife | |
go 1.21.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment