-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to show first N lines (--head=N) #359
Comments
For something like this, why even use ack vs. grep? There are no advantages to ack over grep, other than the Perl regexes. If all you really want is the headings from
|
I'm not sure why ack should need to know anything about the text to always show the first line, i.e. print the first line (whatever it contains) and then proceed to filter from line 2 onwards. Since ack has so many more features than grep, I thought that this could be nice to have too, but I understand your reluctance to add this feature. Cheers, and thanks for your excellent work! |
Hmm, I think I've been mixing up two things. Let's explore this idea. |
So say we have a call like
That means that ack will show the first line that matches, plus any lines that match /foo/. Questions:
|
Also, back to my original question: Why use ack at all for this? Why not do:
|
because i want Perl RE not egrep RE ? ( I use |
I know why you might, but I was asking @alexm. |
Yes, only if there's a match in the file.
Nothing, --head=1 would mean that the first N lines are skipped from filtering as would happen with
Yes. For instance (see the notes below): $ ps -a > ps.txt
$ kubectl get pods > kubectl.txt
$ ack --head=1 foobar *.txt
ps.txt
------
PID TTY TIME CMD
6727 tty2 00:00:51 foobar
kubectl.txt
-----------
NAME READY STATUS RESTARTS AGE
foobar-backend-7d8965b74b-wx76t 1/1 Running 0 2d20h Notes:
I use ack more often that grep, even grep has now the
|
I get all those reasons for using ack over grep (I've preached them :-)) I was just meaning in the case of filtering output from |
In Then, I imagined myself adding a new option to ack (which would be easier for me than doing it for grep). I'm even willing to send a pull request if I find enough round tuits. Other than these, I can't say there's any other particular reason to prefer ack over grep to filter ps output. |
The lines of dashes you've shown would be a new feature, right? Right now if you don't want the grouping/line numbers, you have the I wouldn't want --head=1 to change any behaviors on how things get output. If you were doing an ack of multiple files and you wanted --head=1, you would probably also have to have a --no-line-number argument as well.
When you say "skipped from filtering" here, do you mean "skipped from being searched"? If so, then I'm not sure I'm OK with that, but will think. If not, please say more about what you mean by "filtering"? (And thanks for taking the time to work through these questions. This is the tough part of figuring out features.) |
Some things I'm thinking about: You're talking about using this to show the first line of a stream because you know it's a command like For example, I might go acking through a tree of source and do
because it's helpful to see the first 5 lines of the file that I'm getting results for, even though they aren't a "heading" like in the
Having those first 5 lines helps give me context for the actual matches. And that said, I think that if "salestax" appears in the first 5 lines, then it should be highlighted like any other ack match. Another thought: How does --head=N interact with --output? |
Wow! Didn't know that trick and I like it a lot 😄 I'm assuming that the Thanks, @n1vux! |
@alexm , yes, the scalar Range Operator implicitly compares an integer against On the theory of making simple things simple and hard things easier, (Perl Range Op is more flexible: either value can also be a RE |
Right, but it was just an example of what could be done to highlight the filename without breaking the column layout for commands like ps and the like.
Makes sense. What I'm sensing is that --head=1 has its own place and that some other option
Yes, I meant that, but after reading the case you made later about showing the first N lines of the source files that match a pattern, I guess it makes more sense to search there too. |
Agreed.
I changed my mind, you're right.
Good question. My feeling is that when somebody combines both options is because they expect both to be performed. Otherwise, one of them should be removed. Taking the example of the first 5 lines to add context:
|
I just realized, maybe |
That was my first thought 😄 Is there any other option that is mutually exclusive with --output? i.e. to be coherent regarding its intent. |
Yes, many mutually exclusive options. See |
I don't see a statement of default N, maybe i missed it skimming through. |
Can we set flags in |
A common use of
ack
is filtering the output of commands likeps
,kubectl
, etc. that show column names on the first line:Sometimes it would be useful to show the column names (i.e. usually the first line is enough) when the output meaning is not obvious. This can be easily achieved by adding a match for the first line, e.g.:
However, an option to always show the first line would be very useful, e.g.
ps -a | ack --column-names gnome
ps -a | ack --first-line gnome
The text was updated successfully, but these errors were encountered: