ack3 icon indicating copy to clipboard operation
ack3 copied to clipboard

Add field support (a la Perl)

Open tnelson-doghouse opened this issue 2 years ago • 9 comments

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,

tnelson-doghouse avatar Jan 31 '23 00:01 tnelson-doghouse

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

petdance avatar Jan 31 '23 01:01 petdance

Exactly! Just like the Perl code does.

tnelson-doghouse avatar Jan 31 '23 05:01 tnelson-doghouse

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

petdance avatar Jan 31 '23 06:01 petdance

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.)

n1vux avatar Jan 31 '23 15:01 n1vux

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

petdance avatar Jan 31 '23 15:01 petdance

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,

tnelson-doghouse avatar Jan 31 '23 23:01 tnelson-doghouse

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?

petdance avatar Feb 01 '23 00:02 petdance

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.

tnelson-doghouse avatar Feb 02 '23 00:02 tnelson-doghouse

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

petdance avatar Feb 03 '23 01:02 petdance