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!

1
test=$(
    sed '
        /InitialPattern:/! d
        n
        n
        n
        $!n
        /.*Installation[0-9]\+$/d
        s/.*/FAIL/p
        q
        ' file.txt)
test=${test:-PASS}
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.

  • possibly miss 4th n – Costas Oct 28 '15 at 21:22
  • @Costas - what 4th? The 4th line is 3 next lines away from this one. oh. but asker wants the 5th line. thanks, man. – mikeserv Oct 29 '15 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.