Skip to content
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 field support (a la Perl) #366

Open
tnelson-doghouse opened this issue Jan 31, 2023 · 9 comments
Open

Add field support (a la Perl) #366

tnelson-doghouse opened this issue Jan 31, 2023 · 9 comments
Labels

Comments

@tnelson-doghouse
Copy link

ls -laF | perl -ane 'if($F[2] =~ /tnelson/){print}'

If there was a --field 2 option, then I could do this as:

ls -laF | ack --field 2 tnelson

You might also want to pair this with -F / --separator (as per perl's -F)

HTH,

@petdance
Copy link
Collaborator

So what would ack --field 2 tnelson do? Do a regex match against the 2nd field for /tnelson/ ?

@tnelson-doghouse
Copy link
Author

tnelson-doghouse commented Jan 31, 2023

Exactly! Just like the Perl code does.

@petdance
Copy link
Collaborator

What's your use case? What are you doing such that this feature would be useful?

@n1vux
Copy link
Contributor

n1vux commented Jan 31, 2023

Use Case -- This is searching tab-, space-, comma-, or whatever-delimited data files, subsuming the function of csv-grep (which if we don't list on the alternatives BeyondGrep page we should!)

If we hold hard to the "ack is for searching code, off-label usage tolerated but not supported" mantra, this is an easy "nope, use Text::CSV or awk or csv-grep for that use case".

( OTOH i'm generally in favor of taking a first step, any first step, on the slippery slope towards supporting linguistic use.
--field 2 is roughly the inverse of my coveted --paragraph or -000 mode -- generalizing selection of unit to display and unit to search.)

@petdance
Copy link
Collaborator

I can certainly imagine use cases, but I'm asking what @tnelson-doghouse 's use case is that brought it up.

@tnelson-doghouse
Copy link
Author

The thing that led to this ticket was the fact that I've been writing that Perl about once a week/fortnight for years. In the most recent case, I had a program that would output a list of client e-mails and the websites they were allowed to access. I wanted to search for all e-mail addresses for a particular client, but because the string was also in the website, it was also listing eg. all our internal users with access to that website as well. I wanted to search on just the e-mail field.

Fairly specific use case, but I write that same thing so often I thought it might be a useful feature for others.

HTH,

@petdance
Copy link
Collaborator

petdance commented Feb 1, 2023

So it sounds like you're not using the acky things that make ack ack, like all the pretty color coding.

If I'm understanding right, your suggested

ack --field 2 tnelson

would be the same as

awk '$2 =~ /tnelson/'

and would have identical output, yes?

@tnelson-doghouse
Copy link
Author

tnelson-doghouse commented Feb 2, 2023

I think so, yes. I learned Perl early in my Unix journey, and never ended up doing much awk. Except maybe with better colouring and the like. This would be more relevant with things like:

grep -C 3 regex filename

Then you'd still get the highlighting, which could be handy.

@petdance
Copy link
Collaborator

petdance commented Feb 3, 2023

I'm not really interested in doing this, but I'm not saying no yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants