Issue
I want to use grep -f in a loop but it's not seeing the files I give to -f. My grep version from grep -V
:
grep (GNU grep) 3.6
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others; see
<https://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
Example:
echo "line1" > searchfile.1
echo "line2" > searchfile.2
echo "line1" > targetfile
for file in `ls searchfile*`; do echo $file; ls $file; grep -f $file targetfile; done
gives the output
searchfile.1
ls: cannot access ''$'\033''[0m'$'\033''[32msearchfile.1'$'\033''[0m': No such file or directory
grep: searchfile.1: No such file or directory
searchfile.2
ls: cannot access ''$'\033''[32msearchfile.2'$'\033''[0m': No such file or directory
grep: searchfile.2: No such file or directory
But if I do it manually like
grep -f searchfile.1 targetfile
I get
line1
Any ideas what could be going on?
Solution
Don't parse ls
output, use find
:
find . -mindepth 1 -maxdepth 1 -name 'searchfile*' -exec echo {} \; -exec grep -f {} targetfile \;
Output:
./searchfile.2
./searchfile.1
line1
ls
outputs other characters in addition to the file names (for the colored ls
output). Plus, there may be whitespace in the file names (not in your case, though). See also:
Why not parse ls
(and what to do instead)? - https://unix.stackexchange.com/q/128985/13411
Answered By - Timur Shtatland Answer Checked By - Mildred Charles (WPSolving Admin)