Skip to main content
Updated to only return the last result
Source Link
Zero
  • 93
  • 5

Here's an very simple example on how to achieve it withusing both grep and sed:

#!/usr/bin/bash result=$(grep -o 'nt$' 'test'Output.txt' | sed '$!d') if [ "$result" = 'nt' ]; then echo 'Matched !' else echo 'Not found !' fi 

Explain:

  1. -o Prints only the matched part.
  2. $ Matches at every ending of line
  • -o Prints only the matched part.
  • $ Matches at every ending of line
  • '$!d' Deletes all lines except the last one.

Here's an very simple example on how to achieve it with grep:

#!/usr/bin/bash result=$(grep -o 'nt$' 'test.txt') if [ "$result" = 'nt' ]; then echo 'Matched !' else echo 'Not found !' fi 

Explain:

  1. -o Prints only the matched part.
  2. $ Matches at every ending of line

Here's an very simple example on how to achieve it using both grep and sed:

#!/usr/bin/bash result=$(grep -o 'nt$' 'Output.txt' | sed '$!d') if [ "$result" = 'nt' ]; then echo 'Matched !' else echo 'Not found !' fi 

Explain:

  • -o Prints only the matched part.
  • $ Matches at every ending of line
  • '$!d' Deletes all lines except the last one.
Source Link
Zero
  • 93
  • 5

Here's an very simple example on how to achieve it with grep:

#!/usr/bin/bash result=$(grep -o 'nt$' 'test.txt') if [ "$result" = 'nt' ]; then echo 'Matched !' else echo 'Not found !' fi 

Explain:

  1. -o Prints only the matched part.
  2. $ Matches at every ending of line