Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Searching tabs in a list of files

by haoess (Curate)
on Aug 25, 2005 at 12:18 UTC ( #486521=CUFP: print w/replies, xml ) Need Help??

Sometimes you want to find files with tabs (\t) within their content. With most greps this is a difficult task:

$ bash $ TAB=$(printf "\t") $ grep -Hn "$TAB" *

Using perl, there is another problem: If you want to see the line number, you usually use the special $. variable. But:

Because <> never does an explicit close, line numbers increase across ARGV files.

So you have to close ARGV manually:

$ perl -lne 'print "$ARGV:$.:$_" if /\t/ } continue { close ARGV if eo +f } {' *

-- Frank

Replies are listed 'Best First'.
Re: Searching tabs in a list of files
by davidrw (Prior) on Aug 25, 2005 at 12:42 UTC
    I've never had occaison to run into that behavior -- very handy thing to be aware of though! I just had a few TMTOWTDI comments:

    This is still easy with grep if you know how to generate the tab character:
    grep -Hn '<TAB>' *
    Where "<TAB>" represents a tab character. To type the tab, first hit ctrl-v, then hit TAB -- that should insert an actual tab character. (note it probably won't copy/paste correctly, so be careful if you're copying the grep command from a notes file or something)

    Also, cat can be useful here as well (-n shows line numbers, -T shows TABs as "^I"):
    cat -nT /tmp/foo | grep '\^I'

    Couple alternative perl ways:
    perl -lne 'print "$ARGV:$.:$_" if /\t/; close ARGV if eof' *
    Identical to yours except i believe that the ... } continue { ... } { construct is unnecessary here and confusing at first glance until the reader remembers (if they know) that this is inserted (because of the -n) in a while (<>) { ... } loop.

    Since the original problem is that the line numbers increase accross ARGV files, this, while simple, isn't sufficient (works for only a single file) ...
    perl -ne 'print "$ARGV:$.:$_" if /\t/' /tmp/foo
    ... so Yet Another Alternative :) would be to just wrap that in a shell (e.g. bash here) loop:
    for f in * ; do perl -ne 'print "$ARGV:$.:$_" if /\t/' $f; done
    (more awkward/more steps, but potentially useful in certains cases)
Re: Searching tabs in a list of files
by ambrus (Abbot) on Aug 26, 2005 at 09:15 UTC

    As a bash extension, you can do this: grep $'\t' * Also, you can put a literal tab between quotes to the command (in a script, I did that with a literal \x1b once); in the interactive command line, you can insert a tab with c-Vc-I.

    However, I have my cgrep: Egrep clone with function name display installed on my system, so I can just type cgrep '\t' *.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://486521]
Approved by ysth
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-10-22 16:26 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (273 votes). Check out past polls.