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.
Holiday
to the third line in the file specifically, or to the last line? To compareHoliday
toNo Holiday
, you don't need the file at all, and you know the outcome already. – Kusalananda Oct 08 '17 at 09:27"No Holiday"!="Holiday"
always – αғsнιη Oct 08 '17 at 10:45