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.

3
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.

3
  • Thank u for the answer.....Do you know how to do this using grep command
    – Ammu
    Oct 8 '17 at 10:02
  • 1
    @Ammu See update.
    – Kusalananda
    Oct 8 '17 at 10:28
  • @Ammu If this solves your issue, please consider accepting the answer.
    – Kusalananda
    Oct 8 '17 at 11:01

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.