0

$cat list

Hi
welcome
one
two 
good evening

Value1="two"
value2="evening"

For above file values, output should be echo "values are present one after the other line"

Need to know the if condition command to check if both variable values occur one after the other in a file.

If both variable values occur one line after other in a file, then echo some statement.

for example: $cat list Hi two one three good evening

in above condition, both variable value are not present one after the other line so output should be echo "values are not present one after the other line"

  • 1
    You should be able to solve this on your own. This question doesn't belong here. At least click **[edit](https://stackoverflow.com/posts/46036580/edit)** and update the question with some pseudo-code, or code that you tried and failed to run successfully. – iamdanchiv Sep 04 '17 at 12:06
  • Possible duplicate of [How to represent multiple conditions in a shell if statement?](https://stackoverflow.com/questions/3826425/how-to-represent-multiple-conditions-in-a-shell-if-statement) – fancyPants Sep 04 '17 at 12:11
  • i still have not tried because not sure how to define if condition for two variables values occurring one after the other in a file..Searched for it but couldn't find any answer – user245596 Sep 04 '17 at 12:13
  • no..its not for mutiple condition. command should be to detect if both variables are present one after the other line. if they are present, then echo "they are present one after other line" else echo "they are not present one after the other line" – user245596 Sep 04 '17 at 12:16
  • have edited the question with examples – user245596 Sep 04 '17 at 12:23
  • There is no magical command that does that. You have to implement some logic there... – fancyPants Sep 04 '17 at 12:24
  • can it be done by detecting line number of both variable and check if both are one after other ? – user245596 Sep 04 '17 at 12:28
  • I've voted to close this question because it appears to be a request for a recommendation for a tool or solution, rather than a request for assistance with your own code. This makes your question off-topic for StackOverflow. If that assessment was incorrect, and you do indeed want help writing your own code, then please [add your work so far to your question](https://stackoverflow.com/posts/46036580/edit) and I'll happily retract my close vote. – ghoti Sep 05 '17 at 10:25

2 Answers2

0

With awk you could write something like this:

awk -F= '$1=="Value1"{l=NR}$1=="value2"&&NR==l+1{print "ok"}' file
user000001
  • 32,226
  • 12
  • 81
  • 108
0
#!/bin/bash

Value1="two"
value2="evening"

while read line; do
if [[ "$Value1" == *"$line"* ]];then
    read anotherline
    if [[ "$anotherline" == *"$value2"* ]];then
    echo "values are present one after the other line" 
    fi
fi
done < list

If you want exact string match then remove * wildcards and use single brackets

vishalknishad
  • 690
  • 7
  • 12