Documentation
¶
Overview ¶
Package script aims to make it easy to write shell-type scripts in Go, for general system administration purposes: reading files, counting lines, matching strings, and so on.
Index ¶
- type Pipe
- func (p *Pipe) AppendFile(fileName string) (int64, error)
- func (p *Pipe) Basename() *Pipe
- func (p *Pipe) Bytes() ([]byte, error)
- func (p *Pipe) Close() error
- func (p *Pipe) Column(col int) *Pipe
- func (p *Pipe) Concat() *Pipe
- func (p *Pipe) CountLines() (int, error)
- func (p *Pipe) Dirname() *Pipe
- func (p *Pipe) EachLine(process func(string, *strings.Builder)) *Pipe
- func (p *Pipe) Echo(s string) *Pipe
- func (p *Pipe) Error() error
- func (p *Pipe) Exec(cmdLine string) *Pipe
- func (p *Pipe) ExecForEach(cmdTpl string) *Pipe
- func (p *Pipe) ExitStatus() int
- func (p *Pipe) First(lines int) *Pipe
- func (p *Pipe) Freq() *Pipe
- func (p *Pipe) Join() *Pipe
- func (p *Pipe) Last(lines int) *Pipe
- func (p *Pipe) Match(s string) *Pipe
- func (p *Pipe) MatchRegexp(re *regexp.Regexp) *Pipe
- func (p *Pipe) Read(b []byte) (int, error)
- func (p *Pipe) Reject(s string) *Pipe
- func (p *Pipe) RejectRegexp(re *regexp.Regexp) *Pipe
- func (p *Pipe) Replace(search, replace string) *Pipe
- func (p *Pipe) ReplaceRegexp(re *regexp.Regexp, replace string) *Pipe
- func (p *Pipe) SHA256Sum() (string, error)
- func (p *Pipe) SHA256Sums() *Pipe
- func (p *Pipe) SetError(err error)
- func (p *Pipe) Slice() ([]string, error)
- func (p *Pipe) Stdout() (int, error)
- func (p *Pipe) String() (string, error)
- func (p *Pipe) WithError(err error) *Pipe
- func (p *Pipe) WithReader(r io.Reader) *Pipe
- func (p *Pipe) WithStdout(w io.Writer) *Pipe
- func (p *Pipe) WriteFile(fileName string) (int64, error)
- type ReadAutoCloser
Examples ¶
- Args
- Echo
- Exec (Exitstatus)
- Exec (Ok)
- File
- FindFiles
- IfExists (False)
- IfExists (True)
- ListFiles
- Pipe.Basename
- Pipe.Bytes
- Pipe.Column
- Pipe.Concat
- Pipe.CountLines
- Pipe.Dirname
- Pipe.EachLine
- Pipe.Echo
- Pipe.Exec
- Pipe.ExecForEach
- Pipe.ExitStatus
- Pipe.First
- Pipe.Freq
- Pipe.Join
- Pipe.Last
- Pipe.Match
- Pipe.MatchRegexp
- Pipe.Read
- Pipe.Reject
- Pipe.RejectRegexp
- Pipe.Replace
- Pipe.ReplaceRegexp
- Pipe.SHA256Sum
- Pipe.SHA256Sums
- Pipe.Slice
- Pipe.Stdout
- Pipe.String
- Slice
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pipe ¶
type Pipe struct { Reader ReadAutoCloser // contains filtered or unexported fields }
Pipe represents a pipe object with an associated ReadAutoCloser.
func Args ¶ added in v0.7.0
func Args() *Pipe
Args creates a pipe containing the program's command-line arguments, one per line.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Args().Stdout() // prints command-line arguments }
func Echo ¶ added in v0.3.0
Echo returns a pipe containing the supplied string.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("Hello, world!").Stdout() }
Output: Hello, world!
func Exec ¶ added in v0.5.0
Exec runs an external command and returns a pipe containing its combined output (stdout and stderr).
If the command had a non-zero exit status, the pipe's error status will also be set to the string "exit status X", where X is the integer exit status.
For convenience, you can get this value directly as an integer by calling ExitStatus on the pipe.
Even in the event of a non-zero exit status, the command's output will still be available in the pipe. This is often helpful for debugging. However, because String is a no-op if the pipe's error status is set, if you want output you will need to reset the error status before calling String.
Note that Exec can also be used as a filter, in which case the given command will read from the pipe as its standard input.
Example (Exitstatus) ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { p := script.Exec("echo") fmt.Println(p.ExitStatus()) }
Output: 0
Example (Ok) ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Exec("echo Hello, world!").Stdout() }
Output: Hello, world!
func File ¶
File returns a *Pipe that reads from the specified file. This is useful for starting pipelines. If there is an error opening the file, the pipe's error status will be set.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.File("testdata/hello.txt").Stdout() }
Output: hello world
func FindFiles ¶ added in v0.16.0
FindFiles takes a directory path and returns a pipe listing all the files in the directory and its subdirectories recursively, one per line, like Unix `find -type f`. If the path doesn't exist or can't be read, the pipe's error status will be set.
Each line of the output consists of a slash-separated pathname, starting with the initial directory. For example, if the starting directory is "test", and it contains 1.txt and 2.txt:
test/1.txt test/2.txt
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.FindFiles("testdata/multiple_files_with_subdirectory").Stdout() }
Output: testdata/multiple_files_with_subdirectory/1.txt testdata/multiple_files_with_subdirectory/2.txt testdata/multiple_files_with_subdirectory/3.tar.zip testdata/multiple_files_with_subdirectory/dir/.hidden testdata/multiple_files_with_subdirectory/dir/1.txt testdata/multiple_files_with_subdirectory/dir/2.txt
func IfExists ¶ added in v0.14.0
IfExists tests whether the specified file exists, and returns a pipe whose error status reflects the result. If the file doesn't exist, the pipe's error status will be set, and if the file does exist, the pipe will have no error status. This can be used to do some operation only if a given file exists:
IfExists("/foo/bar").Exec("/usr/bin/something")
Example (False) ¶
package main import ( "github.com/bitfield/script" ) func main() { script.IfExists("doesntexist").Echo("found it").Stdout() }
Example (True) ¶
package main import ( "github.com/bitfield/script" ) func main() { script.IfExists("./testdata/hello.txt").Echo("found it").Stdout() }
Output: found it
func ListFiles ¶ added in v0.11.0
ListFiles creates a pipe containing the files and directories matching the supplied path, one per line. The path can be the name of a directory (`/path/to/dir`), the name of a file (`/path/to/file`), or a glob (wildcard expression) conforming to the syntax accepted by filepath.Match (for example `/path/to/*`).
ListFiles does not recurse into subdirectories (use FindFiles for this).
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.ListFiles("testdata/multiple_files_with_subdirectory").Stdout() }
Output: testdata/multiple_files_with_subdirectory/1.txt testdata/multiple_files_with_subdirectory/2.txt testdata/multiple_files_with_subdirectory/3.tar.zip testdata/multiple_files_with_subdirectory/dir
func Slice ¶ added in v0.11.0
Slice returns a pipe containing each element of the supplied slice of strings, one per line.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { input := []string{"1", "2", "3"} script.Slice(input).Stdout() }
Output: 1 2 3
func Stdin ¶ added in v0.6.0
func Stdin() *Pipe
Stdin returns a pipe that reads from the program's standard input.
func (*Pipe) AppendFile ¶ added in v0.4.0
AppendFile appends the contents of the Pipe to the specified file, and closes the pipe after reading. If the file does not exist, it is created.
AppendFile returns the number of bytes successfully written, or an error. If there is an error reading or writing, the pipe's error status is also set.
func (*Pipe) Basename ¶ added in v0.14.0
Basename reads a list of filepaths from the pipe, one per line, and removes any leading directory components from each line. So, for example, `/usr/local/bin/foo` would become just `foo`. This is the complementary operation to Dirname.
If a line is empty, Basename will produce '.'. Trailing slashes are removed. The behaviour of Basename is the same as filepath.Base (not by coincidence).
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { input := []string{ "", "/", "/root", "/tmp/example.php", "/var/tmp/", "./src/filters", "C:/Program Files", } script.Slice(input).Basename().Stdout() }
Output: . / root example.php tmp filters Program Files
func (*Pipe) Bytes ¶ added in v0.8.0
Bytes returns the contents of the Pipe as a slice of byte, or an error. If there is an error reading, the pipe's error status is also set.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { data, err := script.Echo("hello").Bytes() if err != nil { panic(err) } fmt.Println(data) }
Output: [104 101 108 108 111]
func (*Pipe) Close ¶
Close closes the pipe's associated reader. This is always safe to do, because pipes created from a non-closable source will have an `ioutil.NopCloser` to call.
func (*Pipe) Column ¶ added in v0.9.0
Column reads from the pipe, and returns a new pipe containing only the Nth column of each line in the input, where '1' means the first column, and columns are delimited by whitespace. Specifically, whatever Unicode defines as whitespace ('WSpace=yes'). If there is an error reading the pipe, the pipe's error status is also set.
Lines containing less than N columns will be ignored.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { input := []string{ "PID TT STAT TIME COMMAND", " 1 ?? Ss 873:17.62 /sbin/launchd", " 50 ?? Ss 13:18.13 /usr/libexec/UserEventAgent (System)", " 51 ?? Ss 22:56.75 /usr/sbin/syslogd", } script.Slice(input).Column(1).Stdout() }
Output: PID 1 50 51
func (*Pipe) Concat ¶ added in v0.8.0
Concat reads a list of filenames from the pipe, one per line, and returns a pipe that reads all those files in sequence. If there are any errors (for example, non-existent files), these will be ignored, execution will continue, and the pipe's error status will not be set.
This makes it convenient to write programs that take a list of input files on the command line. For example:
script.Args().Concat().Stdout()
The list of files could also come from a file:
script.File("filelist.txt").Concat()
...or from the output of a command:
script.Exec("ls /var/app/config/").Concat().Stdout()
Each input file will be closed once it has been fully read. If any of the files can't be opened or read, `Concat` will simply skip these and carry on, without setting the pipe's error status. This mimics the behaviour of Unix `cat`.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { input := []string{ "testdata/test.txt", "testdata/doesntexist.txt", "testdata/hello.txt", } script.Slice(input).Concat().Stdout() }
Output: This is the first line in the file. Hello, world. This is another line in the file. hello world
func (*Pipe) CountLines ¶
CountLines counts lines from the pipe's reader, and returns the integer result, or an error. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { n, err := script.Echo("a\nb\nc\n").CountLines() if err != nil { panic(err) } fmt.Println(n) }
Output: 3
func (*Pipe) Dirname ¶ added in v0.14.0
Dirname reads a list of pathnames from the pipe, one per line, and returns a pipe that contains only the parent directories of each pathname. For example, `/usr/local/bin/foo` would become just `/usr/local/bin`. This is the complementary operation to Basename.
If a line is empty, Dirname will produce a '.'. Trailing slashes are removed, unless Dirname returns the root folder. The behaviour of Dirname is the same as filepath.Dir (not by coincidence).
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { input := []string{ "", "/", "/root", "/tmp/example.php", "/var/tmp/", "./src/filters", "C:/Program Files", } script.Slice(input).Dirname().Stdout() }
Output: . / / /tmp /var ./src C:
func (*Pipe) EachLine ¶ added in v0.3.0
EachLine calls the specified function for each line of input, passing it the line as a string, and a *strings.Builder to write its output to. The return value from EachLine is a pipe containing the contents of the strings.Builder.
Example ¶
package main import ( "strings" "github.com/bitfield/script" ) func main() { script.File("testdata/test.txt").EachLine(func(line string, out *strings.Builder) { out.WriteString("> " + line + "\n") }).Stdout() }
Output: > This is the first line in the file. > Hello, world. > This is another line in the file.
func (*Pipe) Echo ¶ added in v0.2.0
Echo returns a pipe containing the supplied string.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.NewPipe().Echo("Hello, world!").Stdout() }
Output: Hello, world!
func (*Pipe) Exec ¶ added in v0.5.0
Exec runs an external command, sending it the contents of the pipe as input, and returns a pipe containing the command's combined output (`stdout` and `stderr`). The effect of this is to use the external command as a filter on the pipe.
If the command had a non-zero exit status, the pipe's error status will also be set to the string "exit status X", where X is the integer exit status.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("Hello, world!").Exec("tr a-z A-Z").Stdout() }
Output: HELLO, WORLD!
func (*Pipe) ExecForEach ¶ added in v0.15.0
ExecForEach runs the supplied command once for each line of input, and returns a pipe containing the output. The command string is interpreted as a Go template, so `{{.}}` will be replaced with the input value, for example. If any command resulted in a non-zero exit status, the pipe's error status will also be set to the string "exit status X", where X is the integer exit status.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("a\nb\nc\n").ExecForEach("echo {{.}}").Stdout() }
Output: a b c
func (*Pipe) ExitStatus ¶ added in v0.5.0
ExitStatus returns the integer exit status of a previous command, if the pipe's error status is set, and if the error matches the pattern "exit status %d". Otherwise, it returns zero.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { p := script.Exec("echo") fmt.Println(p.ExitStatus()) }
Output: 0
func (*Pipe) First ¶ added in v0.9.0
First reads from the pipe, and returns a new pipe containing only the first N lines. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("a\nb\nc\n").First(2).Stdout() }
Output: a b
func (*Pipe) Freq ¶ added in v0.9.0
Freq reads from the pipe, and returns a new pipe containing only unique lines from the input, prefixed with a frequency count, in descending numerical order (most frequent lines first). Lines with equal frequency will be sorted alphabetically. If there is an error reading the pipe, the pipe's error status is also set.
This is a common pattern in shell scripts to find the most frequently-occurring lines in a file:
sort testdata/freq.input.txt |uniq -c |sort -rn
Freq's behaviour is like the combination of Unix `sort`, `uniq -c`, and `sort -rn` used here. You can use Freq in combination with First to get, for example, the ten most common lines in a file:
script.Stdin().Freq().First(10).Stdout()
Like `uniq -c`, Freq left-pads its count values if necessary to make them easier to read:
10 apple
4 banana 2 orange 1 kumquat
Example ¶
package main import ( "strings" "github.com/bitfield/script" ) func main() { input := strings.Join([]string{ "apple", "orange", "banana", "banana", "apple", "orange", "kumquat", "apple", "orange", "apple", "banana", "banana", "apple", "apple", "orange", "apple", "apple", "apple", "apple", }, "\n") script.Echo(input).Freq().Stdout() }
Output: 10 apple 4 banana 4 orange 1 kumquat
func (*Pipe) Join ¶ added in v0.7.0
Join reads the contents of the pipe, line by line, and joins them into a single space-separated string. It returns a pipe containing this string. Any terminating newline is preserved.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("hello\nworld\n").Join().Stdout() }
Output: hello world
func (*Pipe) Last ¶ added in v0.12.0
Last reads from the pipe, and returns a new pipe containing only the last N lines. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("a\nb\nc\n").Last(2).Stdout() }
Output: b c
func (*Pipe) Match ¶ added in v0.2.0
Match reads from the pipe, and returns a new pipe containing only lines that contain the specified string. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("a\nb\nc\n").Match("b").Stdout() }
Output: b
func (*Pipe) MatchRegexp ¶ added in v0.3.0
MatchRegexp reads from the pipe, and returns a new pipe containing only lines that match the specified compiled regular expression. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "regexp" "github.com/bitfield/script" ) func main() { re := regexp.MustCompile("w.*d") script.Echo("hello\nworld\n").MatchRegexp(re).Stdout() }
Output: world
func (*Pipe) Read ¶ added in v0.8.1
Read reads up to len(b) bytes from the data source into b. It returns the number of bytes read and any error encountered. At end of file, or on a nil pipe, Read returns 0, io.EOF.
Unlike most sinks, Read does not necessarily read the whole contents of the pipe. It will read as many bytes as it takes to fill the slice.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { buf := make([]byte, 12) n, err := script.Echo("hello world\n").Read(buf) if err != nil { panic(err) } fmt.Println(n) }
Output: 12
func (*Pipe) Reject ¶ added in v0.2.0
Reject reads from the pipe, and returns a new pipe containing only lines that do not contain the specified string. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("a\nb\nc\n").Reject("b").Stdout() }
Output: a c
func (*Pipe) RejectRegexp ¶ added in v0.3.0
RejectRegexp reads from the pipe, and returns a new pipe containing only lines that don't match the specified compiled regular expression. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "regexp" "github.com/bitfield/script" ) func main() { re := regexp.MustCompile("w.*d") script.Echo("hello\nworld\n").RejectRegexp(re).Stdout() }
Output: hello
func (*Pipe) Replace ¶ added in v0.10.0
Replace filters its input by replacing all occurrences of the string `search` with the string `replace`. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("a\nb\nc\n").Replace("b", "replacement").Stdout() }
Output: a replacement c
func (*Pipe) ReplaceRegexp ¶ added in v0.10.0
ReplaceRegexp filters its input by replacing all matches of the compiled regular expression `re` with the replacement string `replace`. Inside `replace`, $ signs are interpreted as in regexp.Expand, so for instance "$1" represents the text of the first submatch. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "regexp" "github.com/bitfield/script" ) func main() { re := regexp.MustCompile("w.*d") script.Echo("hello\nworld\n").ReplaceRegexp(re, "replacement").Stdout() }
Output: hello replacement
func (*Pipe) SHA256Sum ¶ added in v0.17.0
SHA256Sum calculates the SHA-256 of the file from the pipe's reader, and returns the hex-encoded string result, or an error. If there is an error reading the pipe, the pipe's error status is also set.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { sum, err := script.Echo("hello world").SHA256Sum() if err != nil { panic(err) } fmt.Println(sum) }
Output: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
func (*Pipe) SHA256Sums ¶ added in v0.17.0
SHA256Sums reads a list of file paths from the pipe, one per line, and returns a pipe that contains the SHA-256 checksum of each pathname, in hex. If there are any errors (for example, non-existent files), the pipe's error status will be set to the first error encountered, but execution will continue.
Example ¶
package main import ( "github.com/bitfield/script" ) func main() { script.Echo("testdata/test.txt").SHA256Sums().Stdout() }
Output: a562c9c95e2ff3403e7ffcd8508c6b54d47d5f251387758d3e63dbaaa8296341
func (*Pipe) Slice ¶ added in v0.18.0
Slice returns the contents of the pipe as a slice of strings, one element per line, or an error. If there is an error reading the pipe, the pipe's error status is also set.
An empty pipe will produce an empty slice. A pipe containing a single empty line (that is, a single `\n` character) will produce a slice of one element that is the empty string.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { s, err := script.Echo("a\nb\nc\n").Slice() if err != nil { panic(err) } fmt.Println(s) }
Output: [a b c]
func (*Pipe) Stdout ¶ added in v0.6.0
Stdout writes the contents of the pipe to its configured standard output. It returns the number of bytes successfully written, plus a non-nil error if the write failed or if there was an error reading from the pipe. If the pipe has error status, Stdout returns zero plus the existing error.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { n, err := script.Echo("a\nb\nc\n").Stdout() if err != nil { panic(err) } fmt.Println(n) }
Output: a b c 6
func (*Pipe) String ¶
String returns the contents of the Pipe as a string, or an error, and closes the pipe after reading. If there is an error reading, the pipe's error status is also set.
Note that String consumes the complete output of the pipe, which closes the input reader automatically. Therefore, calling String (or any other sink method) again on the same pipe will return an error.
Example ¶
package main import ( "fmt" "github.com/bitfield/script" ) func main() { s, err := script.Echo("hello\nworld").String() if err != nil { panic(err) } fmt.Println(s) }
Output: hello world
func (*Pipe) WithError ¶
WithError sets the pipe's error status to the specified error and returns the modified pipe.
func (*Pipe) WithReader ¶
WithReader takes an io.Reader, and associates the pipe with that reader. If necessary, the reader will be automatically closed once it has been completely read.
func (*Pipe) WithStdout ¶ added in v0.18.2
WithStdout takes an io.Writer, and associates the pipe's standard output with that reader, instead of the default os.Stdout. This is primarily useful for testing.
func (*Pipe) WriteFile ¶ added in v0.4.0
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. If there is an error reading or writing, the pipe's error status is also set.
type ReadAutoCloser ¶ added in v0.8.0
type ReadAutoCloser struct {
// contains filtered or unexported fields
}
ReadAutoCloser represents a pipe source that will be automatically closed once it has been fully read.
func NewReadAutoCloser ¶ added in v0.8.0
func NewReadAutoCloser(r io.Reader) ReadAutoCloser
NewReadAutoCloser returns an ReadAutoCloser wrapping the supplied Reader. If the Reader is not a Closer, it will be wrapped in a NopCloser to make it closable.
func (ReadAutoCloser) Close ¶ added in v0.8.0
func (a ReadAutoCloser) Close() error
Close closes the data source associated with a, and returns the result of that close operation.
func (ReadAutoCloser) Read ¶ added in v0.8.0
func (a ReadAutoCloser) Read(buf []byte) (n int, err error)
Read reads up to len(buf) bytes from the data source into buf. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF. In the EOF case, the data source will be closed.
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
visitors
This program reads an Apache logfile in Common Log Format, like this: 212.205.21.11 - - [30/Jun/2019:17:06:15 +0000] "GET / HTTP/1.1" 200 2028 "https://example.com/ "Mozilla/5.0 (Linux; Android 8.0.0; FIG-LX1 Build/HUAWEIFIG-LX1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.156 Mobile Safari/537.36" It extracts the first column of each line (the visitor IP address), counts the frequency of each unique IP address in the log, and outputs the 10 most frequent visitors in the log.
|
This program reads an Apache logfile in Common Log Format, like this: 212.205.21.11 - - [30/Jun/2019:17:06:15 +0000] "GET / HTTP/1.1" 200 2028 "https://example.com/ "Mozilla/5.0 (Linux; Android 8.0.0; FIG-LX1 Build/HUAWEIFIG-LX1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.156 Mobile Safari/537.36" It extracts the first column of each line (the visitor IP address), counts the frequency of each unique IP address in the log, and outputs the 10 most frequent visitors in the log. |