1

I have a text file I need to verify, and I am trying to figure out how to check that an expected value exists.

An example of my file is

InitialPattern:
   Value1=somevalue
   Value2=somevalue
   Value3=somevalue
   InstallationName=InstallationX

I don't care about lines 2-4, but I need to verify that in line 5

   InstallationName=Installation1

To throw a wrench in it, this line does not always exist, at which point the pattern starts again with

InitialPattern:

What I have so far kind of works, but not in the case that the line does not occur altogether:

      instName="Installation1"
      installationNames=$(cat file.txt | grep InstallationName)
      IFS=$'\n' read -rd '' -a array <<< "$installationNames"
      for element in "${array[@]}"
      do
         if [[ "$element" =~ "$instName" ]]; then
           test="pass"
         else
           test="fail"
           break
         fi
      done

any ideas? I was looking at this post: Print Matching line and nth line from the matched line Where the user got the forth line after an pattern occurrence - I was thinking if I could store this value I could compare it to the expected value, but I am not entirely sure how to store it yet.

Any guidance is welcome!

2 Answers2

1
sed -e'/\n/D;t'       -e:N  \
    -e'/^InitialPattern/ N' \
    -e'/\nInitialPattern/D' \
    -e's/\n/&/4;tD'         \
    -e'$!s//&/; tN'   -e:D  \
    -e's/ *\(\(Installation\)Name=\2[X0-9]\{1,\}\) *$/\1/;D'

I'm not really sure what you want to do with it, but that only prints the last line in a sequence which matches your specifications.

Here. I wrote one that counts. So you can just change the 4 to whatever you want if you need to - the number you enter there is how many newlines must lie between the most recently occurring InitialPattern match and the Installation line match in order to get it to print it.

mikeserv
  • 58,310
1
test=$(
    sed '
        /InitialPattern:/! d
        n
        n
        n
        $!n
        /.*Installation[0-9]\+$/d
        s/.*/FAIL/p
        q
        ' file.txt)
test=${test:-PASS}
Costas
  • 14,916