Skip to main content
Add requested explanation of alternative script.
Source Link
Michaël
  • 844
  • 7
  • 19

If you're interested in the return value of grep, then GNU sed is also the way:

sed '/pattern/h; ${p;x;/./Q0;Q1}'

You can add to the script modifications to the line, e.g.:

sed '/pattern/h; ${p;x;/./Q0;Q1}; s/pattern/<\0>/g'

This checks that pattern appears on the current line, and stores it in the hold buffer if it is. At the end of the file ($), the last line is printed (p), then the hold space and pattern space are swapped (x). Next, the pattern space is tested for emptiness (/./), and if it is nonempty, exit with code 0 (Q0) otherwise with code 1 (Q1).

An alternative is /pattern/{:a $q1; n; ba}, which loops after detecting pattern, but changing the current line can get tricky with this. This works by:

  • /pattern/: the following clause will only be executed when the pattern is detected on a line.
  • :a … ba: this is defining a label a and branching to it; as is, this is an infinite loop.
  • n: prints the current line and loads the next one.
  • $q1: when reaching the end of file ($), exit with code 1.

By default, sed will exit with code 0, so with this expression, if the pattern is detected, the rest of the file is dumped and an exit code of 1 is produced.

If you're interested in the return value of grep, then GNU sed is also the way:

sed '/pattern/h; ${p;x;/./Q0;Q1}'

You can add to the script modifications to the line, e.g.:

sed '/pattern/h; ${p;x;/./Q0;Q1}; s/pattern/<\0>/g'

This checks that pattern appears on the current line, and stores it in the hold buffer if it is. At the end of the file ($), the last line is printed (p), then the hold space and pattern space are swapped (x). Next, the pattern space is tested for emptiness (/./), and if it is nonempty, exit with code 0 (Q0) otherwise with code 1 (Q1).

An alternative is /pattern/{:a $q1; n; ba}, which loops after detecting pattern, but changing the current line can get tricky with this.

If you're interested in the return value of grep, then GNU sed is also the way:

sed '/pattern/h; ${p;x;/./Q0;Q1}'

You can add to the script modifications to the line, e.g.:

sed '/pattern/h; ${p;x;/./Q0;Q1}; s/pattern/<\0>/g'

This checks that pattern appears on the current line, and stores it in the hold buffer if it is. At the end of the file ($), the last line is printed (p), then the hold space and pattern space are swapped (x). Next, the pattern space is tested for emptiness (/./), and if it is nonempty, exit with code 0 (Q0) otherwise with code 1 (Q1).

An alternative is /pattern/{:a $q1; n; ba}, which loops after detecting pattern, but changing the current line can get tricky with this. This works by:

  • /pattern/: the following clause will only be executed when the pattern is detected on a line.
  • :a … ba: this is defining a label a and branching to it; as is, this is an infinite loop.
  • n: prints the current line and loads the next one.
  • $q1: when reaching the end of file ($), exit with code 1.

By default, sed will exit with code 0, so with this expression, if the pattern is detected, the rest of the file is dumped and an exit code of 1 is produced.

Source Link
Michaël
  • 844
  • 7
  • 19

If you're interested in the return value of grep, then GNU sed is also the way:

sed '/pattern/h; ${p;x;/./Q0;Q1}'

You can add to the script modifications to the line, e.g.:

sed '/pattern/h; ${p;x;/./Q0;Q1}; s/pattern/<\0>/g'

This checks that pattern appears on the current line, and stores it in the hold buffer if it is. At the end of the file ($), the last line is printed (p), then the hold space and pattern space are swapped (x). Next, the pattern space is tested for emptiness (/./), and if it is nonempty, exit with code 0 (Q0) otherwise with code 1 (Q1).

An alternative is /pattern/{:a $q1; n; ba}, which loops after detecting pattern, but changing the current line can get tricky with this.