Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 877
Alan Avatar asked Jun 24 '15 15:06

Alan


2 Answers

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?

like image 80
jim mcnamara Avatar answered Sep 25 '22 04:09

jim mcnamara


Error #1:

When I do var1=$(grep -s "Completed" $line) why does it find all instances of Completed

In the above command grep expects $line to be a filename not a string. If you want to pass string you need to use pipe:

var1=$(echo "$line" | grep -s "Completed")

Or in Bash you can use string redirection:

var1=$(grep -s "Completed" <<<"$line")

Error #2:

There should be space between if and [ ([ is test command)


This grep command will probably do the same as you are trying to do with that code:
grep -v "Completed" file.txt | grep -c "Script Finished"

grep -v "Completed" file.txt returns the lines not containing "Completed" and sends to the next grep through the pipe which returns the line count which contain the text "Script Finished".

like image 37
Jahid Avatar answered Sep 24 '22 04:09

Jahid