Reading a text file line by line and checking if this line has a specific line

I am trying to read a text file line by line using Shell Scripting. What I do 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 for too long.

TO COMPLETE:

I look at the file line by line (while loop), then I meet this 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). Therefore, when I do checks if the variable is completed, I want to exit the while loop if I do not check if the "Script Finished" is a substring, so I can increase the counter (I try to count how many scripts are completed to "Complete").

THE CONFIRMATION:

When I do var1 = $ (grep -s "Completed" $ line), why does it find all instances of Completed ... I thought that if im would go through the lines, it would find only instances of that particular line.

EDIT:

I used the awk answer below. All I had to do was delete the {next} operator, and it works fine.

thanks

+5
source share
3 answers

You have some errors. In addition, you can consider a tool designed for this kind of thing. awk is one of these tools.

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

The first line ends when it matches β€œblah blah” anywhere on the line.

The second line counts the number of Done matches.

The {next} bit is for reading and not printing each line - this happens in some versions of awk.

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

I chose the awk approach, not trying to fix the logic and syntax errors in the shell script. If you need this kind of help, consider playing almost every block (or line) of code on your own at the command line. I assume you used bash.

Error 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, do you test blah blah and expect simple and only blah blah, how is the whole result?

+4
source

Error No. 1:

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

The grep command above expects $line as a file name, not a string. If you want to pass a string, you need to use pipe:

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

Or in Bash, you can use line redirection:

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

Error No. 2:

There must be a space between if and [ ( [ is test )


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

grep -v "Completed" file.txt returns lines that do not contain "Completed", and sends it to the next grep through a pipe that returns the number of lines that contain the text "Script Finished".

+2
source

I post this after the accepted (current) decision, because the decision made is in a language that is not covered by tags.

You can do the same in the shell using the case statement. (although for large files, awk or grep may be faster)

 while read line; do case "$line" in *"Completed"*)break;; *"Script Finished"*)count=$((count + 1));; esac done < file.txt 

note: this form while read line; ... will omit the last line, if not, use while read line || [ "$line" ] while read line || [ "$line" ] (This is one of the reasons some IDEs will add an empty new line to the end of the files)

+1
source

All Articles