### Run script one-liner with goscript.sh Source: https://github.com/bitfield/script/blob/master/README.md Execute a script one-liner using the goscript.sh utility. This example pipes the content of 'file.txt' to the script, which processes it to find the frequency of the first 10 columns. ```sh cat file.txt | ./goscript.sh -c 'script.Stdin().Column(1).Freq().First(10).Stdout()' ``` -------------------------------- ### Sample Log Data Output Source: https://github.com/bitfield/script/blob/master/README.md This is an example of the output format produced after processing log data to count IP address frequencies, showing the count followed by the IP address. ```text 16 176.182.2.191 7 212.205.21.11 1 190.253.121.1 1 90.53.111.17 ``` -------------------------------- ### Custom HTTP Client Configuration Source: https://github.com/bitfield/script/blob/master/README.md Performs an HTTP GET request using a custom `http.Client` with a specified timeout. Useful for fine-tuning request behavior. ```go script.NewPipe().WithHTTPClient(&http.Client{ Timeout: 10 * time.Second, }).Get("https://example.com").Stdout() ``` -------------------------------- ### Fetch Data from HTTP GET Request Source: https://github.com/bitfield/script/blob/master/README.md Fetches data from a given URL using an HTTP GET request and prints the output to standard output. Requires a valid URL. ```go script.Get("https://wttr.in/London?format=3").Stdout() ``` -------------------------------- ### Process JSON Response with JQ Source: https://github.com/bitfield/script/blob/master/README.md Executes an HTTP request, processes the JSON response using a JQ query, and returns the result as a string. Requires JQ to be installed and the `String()` method to retrieve the output. ```go data, err := script.Do(req).JQ(".[0] | {message: .commit.message, name: .commit.committer.name}").String() ``` -------------------------------- ### Import the script library Source: https://github.com/bitfield/script/blob/master/README.md Import the 'script' library to begin using its functionalities for shell-like operations in Go. ```go import "github.com/bitfield/script" ``` -------------------------------- ### Perform HTTP Request with Custom Headers Source: https://github.com/bitfield/script/blob/master/README.md Constructs an HTTP request with custom headers (e.g., Authorization) and executes it using `script.Do`. Requires importing `net/http` and `log`. ```go req, err := http.NewRequest(http.MethodGet, "http://example.com", nil) req.Header.Add("Authorization", "Bearer " + token) script.Do(req).Stdout() ``` -------------------------------- ### Document pipe methods with go doc comments Source: https://github.com/bitfield/script/blob/master/CONTRIBUTING.md Functions and methods require documentation comments explaining inputs, behavior, and outputs. This documentation is used for autogenerated package documentation. ```go // WriteFile writes the contents of the pipe to the specified file, and closes // the pipe after reading. If the file already exists, it is truncated and the // new data will replace the old. It returns the number of bytes successfully // written, or an error. func (p *Pipe) WriteFile(fileName string) (int64, error) { ``` -------------------------------- ### Process Multiple Files from Args Source: https://github.com/bitfield/script/blob/master/README.md Reads multiple files specified as command-line arguments, concatenates their content, filters lines matching a string, and outputs the results to standard output. ```go script.Args().Concat().Match("Error").Stdout() ``` -------------------------------- ### Create a .goscript file with shebang Source: https://github.com/bitfield/script/blob/master/README.md Create a .goscript file that can be executed directly. The shebang line specifies the interpreter, and the subsequent lines define the script's pipeline operations. ```sh #!/tmp/goscript.sh script.Stdin().Column(1).Freq().First(10).Stdout() ``` -------------------------------- ### Execute Command for Each Argument Source: https://github.com/bitfield/script/blob/master/README.md Executes an external command for each command-line argument, passing the argument as input to the command. The `{{.}}` placeholder is replaced by the current argument. ```go script.Args().ExecForEach("ping -c 1 {{.}}").Stdout() ``` -------------------------------- ### Read File Contents as String Source: https://github.com/bitfield/script/blob/master/README.md Reads the entire content of a specified file into a string. Ensure the file exists and is accessible. ```go contents, err := script.File("test.txt").String() ``` -------------------------------- ### Execute External Command Source: https://github.com/bitfield/script/blob/master/README.md Executes an external command (e.g., `ping`) and streams its output to standard output. The command runs concurrently. ```go script.Exec("ping 127.0.0.1").Stdout() ``` -------------------------------- ### Handle errors and nil pipes in methods Source: https://github.com/bitfield/script/blob/master/CONTRIBUTING.md Methods should check for nil pipes or existing errors before proceeding. If an error occurs during an operation, it should be set on the pipe using `SetError`. ```go func (p *Pipe) Frobnicate() *Pipe { // If the pipe has an error, or is nil, this is a no-op if p == nil || p.Error() != nil { return p } output, err := doSomething() if err != nil { // Something went wrong, so save the error in the pipe. The user can // check it afterwards. p.SetError(err) return p } return NewPipe().WithReader(bytes.NewReader(output)) } ``` -------------------------------- ### Limit Output to First N Matches Source: https://github.com/bitfield/script/blob/master/README.md Processes multiple files from command-line arguments, filters lines matching a string, and outputs only the first specified number of matching lines. ```go script.Args().Concat().Match("Error").First(10).Stdout() ``` -------------------------------- ### Count Top 10 Frequent IP Addresses from Stdin Source: https://github.com/bitfield/script/blob/master/README.md This Go program uses the script library to read from standard input, extract the first column (IP address), count the frequency of each IP, and then output the top 10 most frequent ones. It's a direct equivalent to a common shell pipeline. ```go package main import ( "github.com/bitfield/script" ) func main() { script.Stdin().Column(1).Freq().First(10).Stdout() } ``` -------------------------------- ### Tee Output to Terminal and File Source: https://github.com/bitfield/script/blob/master/README.md Outputs data to both standard output (terminal) and a specified file simultaneously. Useful for logging and real-time monitoring. ```go script.Echo("data").Tee().AppendFile("data.txt") ``` -------------------------------- ### Append Filtered Output to File Source: https://github.com/bitfield/script/blob/master/README.md Processes multiple files, filters lines, takes the first N matches, and appends the output to a specified file. Ensure the file path is valid and writable. ```go script.Args().Concat().Match("Error").First(10).AppendFile("/var/log/errors.txt") ``` -------------------------------- ### Grep-like Filtering from Stdin Source: https://github.com/bitfield/script/blob/master/README.md Reads from standard input, filters lines matching a specific string, and outputs the matching lines to standard output. Similar to the `grep` command. ```go script.Stdin().Match("Error").Stdout() ``` -------------------------------- ### Send Data with HTTP POST Request Source: https://github.com/bitfield/script/blob/master/README.md Sends data using an HTTP POST request to a specified URL and outputs the response. Requires a valid URL and data to send. ```go script.Echo(data).Post(URL).Stdout() ``` -------------------------------- ### Line-by-Line Filtering with FilterScan Source: https://github.com/bitfield/script/blob/master/README.md Process input line by line using FilterScan. The provided function receives each line as a string and a writer to send processed output. ```go script.Echo("a\nb\nc").FilterScan(func(line string, w io.Writer) { fmt.Fprintf(w, "scanned line: %q\n", line) }).Stdout() ``` -------------------------------- ### Handle HTTP Errors and Check Response Source: https://github.com/bitfield/script/blob/master/README.md Executes an HTTP request and checks for errors, including non-2xx status codes, which are treated as errors by `script`. Requires importing `log`. ```go _, err := script.Do(req).Stdout() if err != nil { log.Fatal(err) } ``` -------------------------------- ### Filter Lines with a Custom Function Source: https://github.com/bitfield/script/blob/master/README.md Filters lines from standard input using a custom Go function (e.g., `strings.ToUpper`) and outputs the transformed lines. Requires importing the `strings` package. ```go script.Stdin().Match("Error").FilterLine(strings.ToUpper).Stdout() ``` -------------------------------- ### Shell Command for Counting Frequent IP Addresses Source: https://github.com/bitfield/script/blob/master/README.md This shell command pipeline demonstrates a common approach to counting the frequency of IP addresses in a log file using standard Unix utilities like cut, sort, uniq, and head. ```sh cut -d' ' -f 1 access.log |sort |uniq -c |sort -rn |head ``` -------------------------------- ### Count Lines in a File Source: https://github.com/bitfield/script/blob/master/README.md Counts the total number of lines in a specified file. This is useful for analyzing file size or structure. ```go numLines, err := script.File("test.txt").CountLines() ``` -------------------------------- ### Custom Filter with io.Copy Source: https://github.com/bitfield/script/blob/master/README.md Use a custom filter function to process the pipe's output using io.Copy. The function receives a reader for input and a writer for output. ```go script.Echo("hello world").Filter(func (r io.Reader, w io.Writer) error { n, err := io.Copy(w, r) fmt.Fprintf(w, "\nfiltered %d bytes\n", n) return err }).Stdout() ``` -------------------------------- ### Count Lines Matching a String Source: https://github.com/bitfield/script/blob/master/README.md Counts the number of lines in a file that contain a specific string. This is useful for error reporting or log analysis. ```go numErrors, err := script.File("test.txt").Match("Error").CountLines() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.