Search through fortran source for a string ignoring comments I am trying to write a python script that can search through Fortran code
ignoring comments. I didn't find a simple way of doing this using grep. So I startwed writing my own.
The rules for comments that I am following are:

the first character is not a space or a number. It is usually a c, C but I've seen * and other variations
the above condition can be violated if its a fortran90/95 code in that case, its complicated so I am not treating that case for now.
the first non-space character is a &quot;!&quot;

I could not find a simple solution that ignores all types of fortran comments in text so I started writing my own in python 2.x. It looks dirty but works in many cases.
#!/usr/bin/python
import sys
def fgrep(file_name, cond):
      first_valid_chars=tuple(range(0,10 )) + (' ',)
      with open(file_name, 'r') as f:
          for line in f: 
            if line[0] in first_valid_chars:
                line1 = line.strip()
                if line1:    # ignore empty lines
                     if line1[0] != '!':
                         # fortran is case insensitive so comparing lower to lower
                         if cond.lower() in line1.lower():
                               print(line1)


if __name__ == &quot;__main__&quot;:
   print(sys.argv[1])
   file_name = sys.argv[1]
   cond = sys.argv[2]

   fgrep(file_name, cond)

It's drty, not clean and too many indents. How do I clean this up? Because I copy-pasted from editor, there could be errors due to indentation.
Shebang
#!/usr/bin/python is not portable, #!/usr/bin/env python is preferred.
White Space
There should be a blank line between your imports and your function declarations.
Efficiency
You are constantly converting your cond to lowercase.  You don't need to repeatedly do this; just do it once, before the loop.
Right Leaning Code
As you've mention, the code is way-too right leaning.
You could fix this with continue type statements ...
def fgrep(file_name, cond):
    first_valid_chars=tuple(range(0,10 )) + (' ',)

    # Fortran is case insensitive so comparing lower to lower
    cond = cond.lower()
    with open(file_name, 'r') as f:
        for line in f: 
            if line[0] not in first_valid_chars:
                continue

            line1 = line.strip()
            if not line1:    # ignore empty lines
                continue

            if line1[0] == '!':
                continue

            if cond in line1.lower():
                print(line1)

RegEx
You could use a Regular Expression to identify comment lines.

\s*! would find an exclamation mark after any number of spaces.
[^\d\s] would match anything but a digit or a white-space character.

Combined these with the | or-operator, and use .match() to search from the start of the string, and you get an easy comment-line predicate:
def fgrep(file_name, cond):
    # A comment line starts with an exclamation mark as the first non-blank character,
    # or any non-blank character other than a digit as the first character. 
    comment = re.compile(r'\s*!|[^\d\s]')

    # Fortran is case insensitive so comparing lower to lower
    cond = cond.lower()

    with open(file_name, 'r') as f:
        for line in f: 
            if not comment.match(line) and cond in line.lower():
               print(line)

Line numbers
You might want to print out the line number of the matching line, to make it easier to find the lines in the file.  enumerate would help:
def fgrep(file_name, cond):
    # A comment line starts with an exclamation mark as the first non-blank character,
    # or any non-blank character other than a digit as the first character. 
    comment = re.compile(r'\s*!|[^\d\s]')

    # Fortran is case insensitive so comparing lower to lower
    cond = cond.lower()

    with open(file_name, 'r') as f:
        for line_no, line in enumerate(f, 1): 
            if not comment.match(line) and cond in line.lower():
               print(line_no, line, sep=&quot;: &quot;)