0

I have a file with these sentences

Today is a holiday
May I go tomorrow 
No Holiday

From these I need to find No Holiday, and I need to compare that with another string, like grep "No Holiday"!="Holiday".

I don't know the correct query. I am just giving a logic what I need.

αғsнιη
  • 41,407
Ammu
  • 11

1 Answers1

1

Picking out the last line of the file and comparing it to the string Holiday:

holiday_line=$( sed -n '$p' file.txt )
if [ "$holiday_line" = 'Holiday' ]; then
   echo 'The holiday line says "Holiday"'
else
   echo 'The holiday line does not say just "Holiday"'
fi

Or, if you want the third line, change sed -n '$p' to sed -n '3p'. $p means "print the last line" while 3p means "print the 3rd line".

If you want the first found line that contains the string Holiday, no matter where in the file it is, use sed -n '/Holiday/{p;q;}'. The expression /Holiday/{p;q;} means print the line that matches the pattern Holiday, then quit.


Using grep as requested in comments:

if grep -q -wF 'No Holiday' file.txt; then
    echo 'The file contains the string "No Holiday"'
else
    echo '"No Holiday" was not found in the file'
fi

This is similar to my reply to your previous question.

Kusalananda
  • 333,661