Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grep: implement -A context option #810

Merged
merged 1 commit into from
Nov 14, 2024
Merged

Conversation

mknos
Copy link
Contributor

@mknos mknos commented Nov 13, 2024

  • Show non-matching context lines after a matching line
  • A new match may occur within the context lines being displayed; this resets the context counter unless the match limit (-m value) was already reached
  • Display a line of "--" to indicate the end of the context block
  • If -A is used with -n, follow line number with '-' instead of ':' to indicate context lines
  • When -A is used with -m, terminate the search at the end of the context and not the exact line triggering match limit
%perl grep -n -A 8 perl ar # test1: context bleeds into the line8 match
1:#!/usr/bin/perl
2-
3-=begin metadata
4-
5-Name: ar
6-Description: create and maintain library archives
7-Author: dkulp
8:License: perl
9-
10-=end metadata
11-
12-=cut
13-
14-use strict;
15-
16-use POSIX qw(strftime);
--
%perl grep -n -A 8 -m 1 perl ar # test2: line8 match doesn't count due to -m1 limit
1:#!/usr/bin/perl
2-
3-=begin metadata
4-
5-Name: ar
6-Description: create and maintain library archives 
7-Author: dkulp
8:License: perl
9-
--
%perl grep -A 4 perl ar # test3: no -n
#!/usr/bin/perl

=begin metadata

Name: ar
--
License: perl

=end metadata

=cut
--

* Show non-matching context lines after a matching line
* A new match may occur within the context lines being displayed; this resets the context counter unless the match limit (-m value) was already reached
* Display a line of "--" to indicate the end of the context block
* If -A is used with -n, follow line number with '-' instead of ':' to indicate context lines
* When -A is used with -m, terminate the search at the end of the context and not the exact line triggering match limit

%perl grep -n -A 8 perl ar # test1: context bleeds into the line8 match
1:#!/usr/bin/perl
2-
3-=begin metadata
4-
5-Name: ar
6-Description: create and maintain library archives
7-Author: dkulp
8:License: perl
9-
10-=end metadata
11-
12-=cut
13-
14-use strict;
15-
16-use POSIX qw(strftime);
--
%perl grep -n -A 8 -m 1 perl ar # test2: line8 match doesn't count due to -m1 limit
1:#!/usr/bin/perl
2-
3-=begin metadata
4-
5-Name: ar
6-Description: create and maintain library archives
7-Author: dkulp
8:License: perl
9-
--
%perl grep -A 4 perl ar # test3: no -n
#!/usr/bin/perl

=begin metadata

Name: ar
--
License: perl

=end metadata

=cut
--
@github-actions github-actions bot added Type: enhancement improve a feature that already exists Priority: low get to this whenever Program: grep The grep program labels Nov 13, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 11820740906

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.7%) to 73.069%

Totals Coverage Status
Change from base Build 11816683101: -0.7%
Covered Lines: 350
Relevant Lines: 479

💛 - Coveralls

@briandfoy briandfoy added the Type: compatability make it work like GNU or BSD label Nov 14, 2024
@briandfoy briandfoy self-assigned this Nov 14, 2024
@briandfoy briandfoy merged commit 529d42b into briandfoy:master Nov 14, 2024
5 of 23 checks passed
@briandfoy briandfoy removed the Priority: low get to this whenever label Nov 14, 2024
@briandfoy
Copy link
Owner

changes: implement -A for context after a match

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Program: grep The grep program Status: accepted The fix is accepted Type: compatability make it work like GNU or BSD Type: enhancement improve a feature that already exists
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants