lines

command module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2018 License: MIT Imports: 13 Imported by: 0

README

lines

Print a range of lines.

Description

lines is a command line filter that either prints a range of lines, or skips some specified number of lines from the header, some specified number of lines from the footer, or both.

While use of the head and tail POSIX command line programs is already easy to use, how does one go about skipping the initial M lines of a file, or skipping the final N lines of a file? How does one go about skipping both M lines from the top and N lines from the bottom?

Every time I need to do this I spend time doing research on the Internet, judging among a handfull of solutions using awk, perl, python, ruby, or some other scripted solution. Each of the proposals has a slightly different syntax, and some of them don't even work. However all I really want is a filter that does what I need without having to search and study those respective man pages. Is that too much to ask?

I do not claim any of this is an original idea. But I have not found a similar program elsewhere, so I decided to write it myself. I hope it serves others well.

Example

All of the examples assume the following input file, sample.txt.

1: test
2: test
3: test
4: test
5: test
6: test
7: test
8: test
9: test
10: test
Printing a range of lines using '--range START-END'

lines accepts either --range STRING or -r STRING to specify a range of lines to print. In these examples, I always use the short flag.

$ lines sample.txt -r 4-7
4: test
5: test
6: test
7: test

Interestingly, when using the short flag name, one may omit the space between the short flag letter and the argument. Therefore -r 4-7 is the same as -r4-7.

Either or both of the ends of the range parameter may be omitted. When the first number is omitted, printing starts at the first line. When the final number is omitted, printing ends at the final line.

As previously described, the intervening space between the flag letter and the argument may be omitted, causing -r -3 to have the same meaning as -r-3, printing the first 3 lines of the file. Printing the first 3 lines of the file is equivalent to lines --head 3.

$ lines sample.txt -r -3
1: test
2: test
3: test

Both -r 7- and -r7- both print lines 7 thru the end of the file. Note this is different than printing the final 7 lines of the file, as one might do with lines --tail 7.

$ lines sample.txt -r 7-
7: test
8: test
9: test
10: test
Printing a single line using '--range N'
$ lines sample.txt -r 3
3: test
Omitting one or more header lines using '--header N'
$ lines sample.txt --header 2
4: test
5: test
6: test
7: test
8: test
9: test
10: test
$ lines sample.txt --footer 2
1: test
2: test
3: test
4: test
5: test
6: test
7: test
8: test
$ lines sample.txt --header 3 --footer 2
4: test
5: test
6: test
7: test
8: test
Printing only the initial N lines

Duplicates behavior of invoking head -n N, but included here for completeness. Also note this will have the same effect as calling -r -N.

$ lines sample.txt --head 3
1: test
2: test
3: test
Printing only the final N lines

Duplicates behavior of invoking tail -n 3, but included here for completeness.

$ lines sample.txt --tail 3
8: test
9: test
10: test

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL