How to Grep in a Loop

How can I grep in a loop?

There is no need to loop, you can do use grep with the -f option to get patterns from a file:

grep -f pattern_file files*

From man grep:

-f FILE, --file=FILE

Obtain patterns from FILE, one per line. The empty file contains zero
patterns, and therefore matches nothing. (-f is specified by POSIX.)

Test

$ cat a1
hello
how are you?

$ cat a2
bye
hello

$ cat pattern
hello
bye

$ grep -f pattern a*
a1:hello
a2:bye
a2:hello

Grep in a for loop in Linux

Next to the magic command from oguz ismail, you could use the following awk line:

awk '/Thermochemistry/{close(f);f="newfile_"++i".tmp"
for(i=FNR;i<=FNR+(FNR>500?500:FNR);++i) print b[i%500] > f
}{b[FNR%500]=$0}' file

How to process each output line in a loop?

One of the easy ways is not to store the output in a variable, but directly iterate over it with a while/read loop.

Something like:

grep xyz abc.txt | while read -r line ; do
echo "Processing $line"
# your code goes here
done

There are variations on this scheme depending on exactly what you're after.

If you need to change variables inside the loop (and have that change be visible outside of it), you can use process substitution as stated in fedorqui's answer:

while read -r line ; do
echo "Processing $line"
# your code goes here
done < <(grep xyz abc.txt)

Extracting lines from file using grep in a for loop, exporting to new file with variable in file name

Since ...

What this code does is simply make a blank file for every variable.

... you know that your variables file is being read correctly, and your for loop is correctly iterating over the results. That the resulting files are empty indicates that grep is not finding any matches to your pattern.

Why not? Because the pattern in your grep command ...

    grep "'${variable}'" \

... doesn't mean what you appear to think it means. You have taken some pains to get literal apostrophes (') into the pattern, but these have no special meaning in that context. Your pattern does not match any lines because in the data, there are no apostrophes around the appearances of the target strings.

This would be better:

    grep -F -e "${variable}" \

The -F option tells grep to treat the pattern as a fixed string to match, so that nothing within is interpreted as a regex metacharacter. The -e ensures that the pattern is interpreted as such, even if, for example, it begins with a - character. The double quotes remain, as they are required to ensure that the shell does not perform word splitting on the expanded result, and of course the inner apostrophes are gone, since they were causing the main problem.

Unable to grep with value having space in linux inside a for loop

strip the last and first character from a String

Using the above answer I am removing first and last character of variable which are quotes in the command and its showing results now

Modified inside for loop to

sysctl -a | grep -i "${parm:1:-1}";

from

sysctl -a | grep -i "$parm"; 

and it worked like charm

using IF loop to grep for a pattern and execute statements

I just adjusting the path should work

#!/bin/bash
File="error.txt"
for d in *_b;
do
if grep -q "ECHO" $d/"$File"; then
echo "$d:";
Statement 1
echo "------------------";
else
echo "$d:";
Statement 2
echo "------------------";
fi
done

bash script using grep in a for loop

Start by checking what you have.

echo *.xml # should list your files

If this outputs *.xml you're in the wrong place.

Try it this way:

for s in "$SLIDE_PATH"/*.xml
do grep -L ppaction://media "$s" | grep -o -E '[0-9]+'
done

Does that work any better?



Related Topics



Leave a reply



Submit