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
.Htaccess Redirect Index.PHP to /
Qxcbconnection: Could Not Connect to Display Aborted, When Installing Qt on Linux
What Happens Internally When Deleting an Opened File in Linux
Cron Expression to Run on Different Days for Different Months
Analog of Com Programming in Linux/Unix
How I Install Specific Fonts on My Aws Ec2 Instance
Does There Exist Kernel Stack for Each Process
How to Successfully Use Rdap Protocol Instead of Whois
How to Compare Two Text Files for The Same Exact Text Using Bash
Using Dma Memory Transfer in User-Space
Programmatically Disable Hardware Prefetching on Amd Systems
Building Perf with Babeltrace (For Perf to Ctf Conversion)
Ensure Config.H Is Included Once
Linux: Update Embedded Resource from Executable
$${Home} or ${Home} in Makefile
Source Line Numbers in Perf Call Graph
Base Address at Which The Linux Kernel Is Loaded
How to Set Folder Permissions for a Particular Container on Elastic Beanstalk