How to Search a string in a file in shell script?

I have a text file which is generated when the batch job is run. This batch may take few mins to run. When completed, the last line of the text file would be process completed. I need a shell script which will wait for this file till the process completed is printed in it, once found, it would move to the next line else it would wait till found.

I am using AIX cShell.

It looks like the first thing you would do would be the tail command to get the last line, piping the result to a grep or sed. You can then check the return code to see if you found what you needed on the last line if I understood your request.

If you have a script running a process to create a file and something else reading data from that file, you have a few obvious choices:

  • Pipe the output of the process creating the file into the code that processes the data it creates. This works trivially with no coordination between the two processes needed other than the pipe that connects them.
  • Have the first process direct its output to a regular file. When that process completes, start the second process with the file the first process created as an input file.
    Start the process that creates your data with output redirected to a regular file asynchronously. [*]Start the process that reads that data using tail -f to read the file created by the the first process and pipe it into the process that reads the data. Let it read the data until it finds the magic line in the file indicating end-of-data. When it finds that magic line, it can exit which will have the side-effect of terminating the tail -f .

There are millions of variations on these depending on what operating system you're using, what shell you're using, what your processes are doing, what your data looks like, and on all of the details that you have not told us.