Giter VIP home page Giter VIP logo

fields's Introduction

fields

Use the fields CLI command as a modern replacement for awk + cut. With fields you specify which columns of text you want, in a flexible format.

GitHub run-ci

Getting Started

Build from source

The fields command can be installed via Go by running

$ go install github.com/shoenig/fields/cmd/fields@latest

Example Usages

select a single column (from left)

$ fields 3 <<< "a b c d e f g"
c

select a single column (from right)

$ fields -- -3 <<< "a b c d e f g"
e

select multiple columns

$ fields 1,-1,2,-2 <<< "a b c d e f g"
a g b f

select columns to the right of N (from left)

$ fields 4: <<< "a b c d e f g"
d e f g

select columns to the right of N (from right)

$ fields -- -2: <<< "a b c d e f g"
f g

select columns to the left of N (from left)

$ fields :2 <<< "a b c d e f g"
a b

select columns to the left of N (from right)

$ fields :-2 <<< "a b c d e f g"
a b c d e f

select range of columns

$ fields 2:5 <<< "a b c d e f g"
b c d e

any combination of the above, all together

$ fields 1,2,-2,3:5,2:,:-3 <<< "a b c d e f g"
a b f c d e b c d e f g a b c d e

Contributing

The github.com/shoenig/fields module is always improving with new features and error corrections. For contributing bug fixes and new features please file an issue.

License

The github.com/shoenig/fields module is open source under the MIT license.

fields's People

Contributors

dependabot[bot] avatar shoenig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fields's Issues

handling of negative indices brkoe

Some argument handling changes broke the way negative indices are handled:

./fields -- -1 <<< "a b c"
fields - CLI columnar text processor [v1]
	fields is a tool for selecting columns of text. One or more individual columns
	and/or ranges of columns can be selected by index. Indexes can be positive

Need to make them work again

negative values in range operation do not work

just causes a panic

[x1 ~] $ fields 1:-2 <<< "a b c d e f g"
panic: runtime error: index out of range

goroutine 1 [running]:
gophers.dev/cmds/fields.zip(0xc000012440, 0x4, 0x4, 0xc0000a4280, 0x7, 0xa, 0x0, 0xc0000b6000, 0x203000)
	/home/hoenig/go/src/gophers.dev/fields/apply.go:90 +0x17d
gophers.dev/cmds/fields.process(0xc000018480, 0xd, 0x512c60, 0xc00000e018, 0x512b00, 0xc00000c240, 0x8, 0x18)
	/home/hoenig/go/src/gophers.dev/fields/apply.go:52 +0x120
gophers.dev/cmds/fields.Apply(0x512b00, 0xc00000c240, 0x512c40, 0xc00000e010, 0x512c60, 0xc00000e018, 0x0, 0x512ec0)
	/home/hoenig/go/src/gophers.dev/fields/apply.go:24 +0x123
main.do(0x0, 0x0, 0x7fff0e0a41db, 0x4, 0x512c40, 0xc00000e010, 0x512c60, 0xc00000e018, 0x0, 0x0)
	/home/hoenig/go/src/gophers.dev/fields/cmd/fields/command.go:98 +0xef
main.(*command).Execute(0xc00005bf50, 0xc00000c090, 0x1, 0x1, 0x0)
	/home/hoenig/go/src/gophers.dev/fields/cmd/fields/command.go:56 +0x139
main.main()
	/home/hoenig/go/src/gophers.dev/fields/cmd/fields/main.go:18 +0x154

create a snapcraft snap

Would be nice to be able to publish snaps of fields to Canonical's snap catalog, and call that our official distribution channel

support for custom cutsets

Currently fields only chops columns based on whitespace (tabs and spaces), and processes input on newlines. It'd be nice to be able to specify what to chop fields on other than tabs and spaces. Could go with a set of characters, or maybe a full regexp (or both).

add support for left/right expansions

Currently fields can handle column definitions in the form of single digits or spans of digits
(e.g. 1,2,3,4,5 or 1-5). It would be nice if we could have fields print columns like "everything after i'th index" or "everything to left of i'th index". We could use notation like 1+ and -5 to indicate right and left expansions respectively.

options to skip lines

Would be nice if there was...

An option to skip N lines
An option to limit processing to N lines
An option to truncate the last N lines
An option to process only the last N lines

Produce helpful output when running with no args

The fields tool just produces an error when run with no arguments.

$ fields
fatal: expected 1 argument, got 0

An example of a tool that handles this well is jq which outputs help text when run with no arguments.

$ jq
jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: jq [options] <jq filter> [file...]

	jq is a tool for processing JSON inputs, applying the
	given filter to its JSON text inputs and producing the
	filter's results as JSON on standard output.
	The simplest filter is ., which is the identity filter,
	copying jq's input to its output unmodified (except for
	formatting).
	For more advanced filters see the jq(1) manpage ("man jq")
	and/or https://stedolan.github.io/jq

	Some of the options include:
	 -c		compact instead of pretty-printed output;
	 -n		use `null` as the single input value;
	 -e		set the exit status code based on the output;
	 -s		read (slurp) all inputs into an array; apply filter to it;
	 -r		output raw strings, not JSON texts;
	 -R		read raw strings, not JSON texts;
	 -C		colorize JSON;
	 -M		monochrome (don't colorize JSON);
	 -S		sort keys of objects on output;
	 --tab	use tabs for indentation;
	 --arg a v	set variable $a to value <v>;
	 --argjson a v	set variable $a to JSON value <v>;
	 --slurpfile a f	set variable $a to an array of JSON texts read from <f>;
	See the manpage for more options.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.