Skip to main content
added 10 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Using any POSIX sort and awk with the OPs original grep -R:

$ grep -R serialVersionUID | sort -k2 -b |
awk '
    { curr=$0;currKey=$0; sub(/^[^[:space:]]+[[:space:]]+/,"",currcurrKey) }
    currcurrKey == prevprevKey { print firstfirstLine $0; first="";firstLine=""; next }
    { prev=curr;prevKey=currKey; first=$0firstLine=$0 ORS }
'
./path/to/Another.java:        private static final long serialVersionUID = 111L;
./path/to/Some.java:    private static final long serialVersionUID = 111L;

Regarding [ ][ ] - blanks are already literal in a regexp, you don't need to put them inside a bracket expression. /[ ][ ]*/ is identical to / */. Maybe you meant to use /[ \t][ \t]*/ to match repetitions of blanks or tabs? You don't need that sed at all though, just add the -b option to sort as I did above to tell it to ignore leading spaces in a field.

The above assumes the file names output by grep -R don't contain white space. If they can then we'd need a different solution that doesn't start with grep -R.

Using any POSIX sort and awk with the OPs original grep -R:

$ grep -R serialVersionUID | sort -k2 -b |
awk '
    { curr=$0; sub(/^[^[:space:]]+[[:space:]]+/,"",curr) }
    curr == prev { print first $0; first=""; next }
    { prev=curr; first=$0 ORS }
'
./path/to/Another.java:        private static final long serialVersionUID = 111L;
./path/to/Some.java:    private static final long serialVersionUID = 111L;

Regarding [ ][ ] - blanks are already literal in a regexp, you don't need to put them inside a bracket expression. /[ ][ ]*/ is identical to / */. Maybe you meant to use /[ \t][ \t]*/ to match repetitions of blanks or tabs? You don't need that sed at all though, just add the -b option to sort as I did above to tell it to ignore leading spaces in a field.

The above assumes the file names output by grep -R don't contain white space. If they can then we'd need a different solution that doesn't start with grep -R.

Using any POSIX sort and awk with the OPs original grep -R:

$ grep -R serialVersionUID | sort -k2 -b |
awk '
    { currKey=$0; sub(/^[^[:space:]]+[[:space:]]+/,"",currKey) }
    currKey == prevKey { print firstLine $0; firstLine=""; next }
    { prevKey=currKey; firstLine=$0 ORS }
'
./path/to/Another.java:        private static final long serialVersionUID = 111L;
./path/to/Some.java:    private static final long serialVersionUID = 111L;

Regarding [ ][ ] - blanks are already literal in a regexp, you don't need to put them inside a bracket expression. /[ ][ ]*/ is identical to / */. Maybe you meant to use /[ \t][ \t]*/ to match repetitions of blanks or tabs? You don't need that sed at all though, just add the -b option to sort as I did above to tell it to ignore leading spaces in a field.

The above assumes the file names output by grep -R don't contain white space. If they can then we'd need a different solution that doesn't start with grep -R.

added 10 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Using any POSIX cat filesort in place ofand awk with the OPs original grep -R ... for this example:

$ catgrep file-R serialVersionUID | sort -k2 -b |  
awk '
    { curr=$0; sub(/^[^[:space:]]+[[:space:]]+/,"",curr) }
    curr == prev { print first $0; first=""; next }
    { prev=curr; first=$0 ORS }
'
./path/to/Another.java:        private static final long serialVersionUID = 111L;
./path/to/Some.java:    private static final long serialVersionUID = 111L;

Regarding [ ][ ] - blanks are already literal in a regexp, you don't need to put them inside a bracket expression. /[ ][ ]*/ is identical to / */. Maybe you meant to use /[ \t][ \t]*/ to match repetitions of blanks or tabs? You don't need that sed at all though, just add the -b option to sort as I did above to tell it to ignore leading spaces in a field.

The above assumes the file names output by grep -R don't contain white space. If they can then we'd need a different solution that doesn't start with grep -R.

Using cat file in place of grep -R ... for this example:

$ cat file | sort -k2 -b | awk '
    { curr=$0; sub(/^[^[:space:]]+[[:space:]]+/,"",curr) }
    curr == prev { print first $0; first=""; next }
    { prev=curr; first=$0 ORS }
'
./path/to/Another.java:        private static final long serialVersionUID = 111L;
./path/to/Some.java:    private static final long serialVersionUID = 111L;

Regarding [ ][ ] - blanks are already literal in a regexp, you don't need to put them inside a bracket expression. /[ ][ ]*/ is identical to / */. Maybe you meant to use /[ \t][ \t]*/ to match repetitions of blanks or tabs? You don't need that sed at all though, just add the -b option to sort as I did above to tell it to ignore leading spaces in a field.

Using any POSIX sort and awk with the OPs original grep -R:

$ grep -R serialVersionUID | sort -k2 -b | 
awk '
    { curr=$0; sub(/^[^[:space:]]+[[:space:]]+/,"",curr) }
    curr == prev { print first $0; first=""; next }
    { prev=curr; first=$0 ORS }
'
./path/to/Another.java:        private static final long serialVersionUID = 111L;
./path/to/Some.java:    private static final long serialVersionUID = 111L;

Regarding [ ][ ] - blanks are already literal in a regexp, you don't need to put them inside a bracket expression. /[ ][ ]*/ is identical to / */. Maybe you meant to use /[ \t][ \t]*/ to match repetitions of blanks or tabs? You don't need that sed at all though, just add the -b option to sort as I did above to tell it to ignore leading spaces in a field.

The above assumes the file names output by grep -R don't contain white space. If they can then we'd need a different solution that doesn't start with grep -R.

Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Using cat file in place of grep -R ... for this example:

$ cat file | sort -k2 -b | awk '
    { curr=$0; sub(/^[^[:space:]]+[[:space:]]+/,"",curr) }
    curr == prev { print first $0; first=""; next }
    { prev=curr; first=$0 ORS }
'
./path/to/Another.java:        private static final long serialVersionUID = 111L;
./path/to/Some.java:    private static final long serialVersionUID = 111L;

Regarding [ ][ ] - blanks are already literal in a regexp, you don't need to put them inside a bracket expression. /[ ][ ]*/ is identical to / */. Maybe you meant to use /[ \t][ \t]*/ to match repetitions of blanks or tabs? You don't need that sed at all though, just add the -b option to sort as I did above to tell it to ignore leading spaces in a field.