Skip to content
Advertisement

Reading through a text file line by line and checking if that line has a certain string

I am trying to read a text file line by line using Shell Scripting. What I have been doing is

while read line
do
   var1=$(grep -s "Completed" $line)
   var2=$(grep -s "Script Finished" $line)

if[ "$var1" = "$line" ]
   break
else
   if[ "$var2" = "$line" ]
       count='expr $count + 1'
   else
      countinue
   fi
fi
done < file.txt

If you have any suggestions please let me know! I am open to other options because I have been trying to do this way too long.

TO CLARIFY:

I am going through a file line by line (while loop) then I am grepping that line to see if “Completed” is a substring and grepping to see if “Script Finished” is a substring (Grep will set the variable to the whole line). So then when I do the checks if the variable is Completed I want to break out of the while loop if not check if “Script Finished” is a substring so I can increment a counter (I am trying to count how many scripts finished before “Complete”).

CONFUSED ABOUT:

When I do var1=$(grep -s “Completed” $line) why does it find all instances of Completed… I thought if im going through line by line it will only find the instances in that certain line.

EDIT:

I used the awk answer below. All I had to do is remove the {next} statement and it works perfectly.

Thank you

Advertisement

Answer

You have several errors. Plus you may want to consider a tool that is meant to do this kind of thing. awk is one of those tools.

awk '/blah blah/ {exit} 
     /Finished/ {count+=1} 
     {next}
     END{ print count} ' filename

The first line quits when it matches “blah blah” anywhere on the line.

The second line counts the number of “Finished” matches.

The {next} bit is there to keep reading and not print every line – this happens in some versions of awk.

The last line , END {} function, runs when the code completes the file. It displays the value of count.

I chose the awk approach rather than trying to fix logic and syntax errors in the shell script. If you need that kind of help consider playing almost every block (or line) of code all by itself on the command line. I am assuming you used bash.

Errors examples

-eq to compare strings, use =, example:  [ "$var" = "something" ]
$(var1) should be either "${var1}" or "$var1" lines 4 and 8

grep returns a whole line, are you testing “blah blah” and expecting just and only just “blah blah” as the entire result?

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement