4

I'm working in the shell, trying to find NUL chars in a bunch of CSV files (that Python's CSV importer is whinging about, but that's for another time) using the so-proud-of-my-ever-clever-self:

find ~/path/ -name "*.csv" -print0 | \
xargs -n 1 -0 \
perl -ne 'if(m/\x{00}/){print fileno(ARGV).join(" ",@ARGV).$_;}'

Except I see no filename. Allegedly the implicit <> operator that perl -ne is wrapping my script in is just using @ARGV / the ARGV filehandle, but neither of the above is giving me the name of the current file.

How do I see the current filename (and, ideally, line number) in the above?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Why does `xargs -n 1` limit one file per perl process? If you're using `perl -n` mode and not doing anything very sneaky, it would act the same if xargs allowed giving multiple filenames to the same perl. – aschepler May 04 '11 at 23:28
  • Sorry, it was debugging cruft left in there. – pauloppenheim May 05 '11 at 00:11

2 Answers2

10

$ARGV is the name of the current file and $. is the current line number; see perldoc perlvar and I/O Operators in perldoc perlop. (Note that $. doesn't reset between files; there's discussion of that in perldoc -f eof.)

And I'm not entirely sure what you're trying to accomplish with that print; it will give you the filehandle number, which is probably 3, prepended to a space-separated list of filenames (which should probably be only the one because of xargs -n), then the current line which will include the NUL and other potentially terminal-confusing characters.

Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82
geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • The issue was that join(" ",@ARGV) was the empty string - evidently the current file is `shift`ed off first, and $ARGV does in fact hold what I am looking for. Tricksy perlses! And yes it's mostly gibberish, i was debugging. Thanks again! – pauloppenheim May 05 '11 at 00:12
0

Pproceed something like this (I searched .pl files for "x" here):

find -type f -name \*.pl -print0 | \
xargs -0 \
perl -we 'while (<>) { print qq($ARGV\t$.\t$_) if m/x/ }'

And yes, it can be shortened using the -n switch:

find -type f -name \*.pl -print0 | \
xargs -0 \
perl -nwe 'print qq($ARGV\t$.\t$_) if m/x/'
Lumi
  • 14,775
  • 8
  • 59
  • 92