I am doing perl -pe along with grep to do a multi line grep. This is being done so that when "" is used as a line continuation letter, I need to join the line.
So my file is
record -field X \
-field Y
I am doing
perl -pe 's/\\\n/ /' a/b/c/*/records/*.rec | grep "\-field.*X.*\-field.*Y"
The problem with this is that it just gives me the grep result, without telling me which file had the issue. Is there a way around this. I need to know which files have this too.
I can do a foreach shell script, but was wondering if there is a one liner version of the same possibe
CodePudding user response:
Once you are inside a Perl program why go to system's grep
? Perl's tools are far more abundant, rounded, and usable than the shell's. One way
perl -0777 -nE'say "$ARGV: $_" for
grep { /\-field.*X.*\-field.*Y/ } split /\n/, s{\\\n}{ }gr' file-list
(broken into lines for readability)
We read the whole file into $_
("slurp" it), so to be able to merge those particular lines, using the -0777
switch. That \\n
is then substituted with a space and the resulting string returned (by virtue of the /r
modifier), and split
by \n
to regenerate lines.
Then that list of lines is fed to grep with your desired pattern, and the ones that match the pattern are passed through. So then they are printed, prepended with the name of the currently processed file, available in the $ARGV
variable.
CodePudding user response:
The answer is to use ARGV[0]
perl -pe 'print "$ARGV[0]: ";s/\\\n/ /' a/b/c/*/records/*.rec | grep "\-field.*X.*\-field.*Y"