Your loop seems a bit strange, you'd want to loop through all files, not through the set of things you provided (which is a set with a single entry, .
). Not really useful at all.
So,
for file in *
would make much more sense. Then, use that ${file}
variable! You're not even doing anything with it in your for loop! That also makes no sense.
For example, you could
- use
read
to get lines from the file (thousands of examples on how to read lines from files using bash)
- use
cut
to select the position
- use
[[
/ ]]
to test for the string in that position, and if successful
- print the name of the file and skip ahead to the next file.
alternatively, learn your self a bit of regexes. Don't know which version of grep
you have, but "from the beginning of the line, find the things that has N repititions the scheme "any repitititon of anything but a delimiter + one word delimiter followed by the word I'm looking for" isn't hard.
Something like, to look for "mustard" in the fifth word:
words_before=4
word="mustard"
# idea is to get the sed expression '/^\([^ ]\+ \)\{words_before\}word/!{qerror_code}'
sedtemplate_start='/^\([^ ]\+ \)'
sedtemplate_end='/!{q100}'
sedtemplate="${sedtemplate_start}\\{${words_before}\\}${word}${sedtemplate_end}"
#.... open all files, go through all lines
( echo "${this_line}" | sed -n "${sedtemplate}" ) && echo "${file}"