Reputation: 2423
I don't do much shell scripting, but I want to essentially do this:
run the command "grunt check" about 30 times (the process takes 60 seconds).
Do a regex on the output of that command for "Some random error message. Failed." Where "Failed" is the thing I'm searching for but I want to capture the whole sentence.
Write the associated line to a file.
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 30 ]; do
command grunt check
// ERROR = regex(/\/Failed./)
// WRITE ERROR TO FILE
let COUNTER=COUNTER+1
done
Upvotes: 1
Views: 166
Reputation: 1
for ((cr=0; cr<30; cr++))
do
grunt check | grep Failed
done > outfile.txt
Upvotes: 2
Reputation: 113934
counter=0
while [ $counter -lt 30 ]; do
grunt check | grep Failed
let counter=counter+1
done > some file
The above uses a pipeline to capture the output of the grunt
command and sent it to grep
. grep
searches through the output and prints any lines that contain the word Failed
. Any such lines are then sent to a file named somefile
.
As a minor point, I have converted COUNTER to lower case. This is because the system uses upper case environment variables. If you make a practice of using lower case ones then you won't accidentally overwrite one. (In this particular case, there is no system variable named COUNTER, so you are safe.)
You might find this simpler:
for counter in {1..30}; do
grunt check | grep Failed
done > somefile
The {1..30}
notation provides the numbers from one to thirty. It is a bash feature so don't try to use it on a bare-bones POSIX shell.
If you would like to see more context around the error message, grep
offers several options to help. To see both the line matching "Failed" and the line before, use -B
:
for counter in {1..30}; do
grunt check | grep -B 1 Failed
done >somefile
Similarly, -A
can be used to display lines after the match. -C
will display lines both before and after the match.
Upvotes: 1