Skip to main content
9 votes

How can I find common prefixes in file names to group them?

With perl: $ find . -print0 | perl -C -l -0ne ' if (m{/([^/]{4,}?\w)\b[^/]*\z}) { push @{$list{$1}}, $_; } END { while (($part, $list) = each(%list)) { if (($count = @$list) > 1)...
Stéphane Chazelas's user avatar
5 votes
Accepted

Help w/ "posix-extended" regex for 'find'

If you have zsh, you don't need find (and BTW, FreeBSD's find is not old, it's just a different implementation from the GNU one, where extended regexps is just with -E like in grep or sed). find . -...
Stéphane Chazelas's user avatar
4 votes

Help w/ "posix-extended" regex for 'find'

In addition to @muru's answer pointing out that find doesn't support \s (so you need to use [:space:] instead): Instead of A-Za-z, you can use the posix character class [:alpha:], and instead of A-Za-...
cas's user avatar
  • 83.9k
3 votes

How can I find common prefixes in file names to group them?

If you only accept the string to end at a word border then this gives you the potential common strings; tune that to your requirements: sed -n '/^\(\w\+ \w\+\) /s/^\(\w\+ \w\+\) .*$/\1/p' file Then ...
Hauke Laging's user avatar
  • 94.5k
3 votes

Help w/ "posix-extended" regex for 'find'

Two things: \s is not supported by GNU find. There are \w and \W for word and non-word, but no \s. See the documentation. Also: Why does my regular expression work in X but not in Y? on why the ...
muru's user avatar
  • 77.9k
2 votes

How can I find common prefixes in file names to group them?

Using Raku (formerly known as Perl_6) Raku regex below groups filenames by starting characters: minimum 3-or-more <alpha> characters followed by \h horizontal whitespace or _ underscore: ~$ ...
jubilatious1's user avatar
  • 3,903

Only top scored, non community-wiki answers of a minimum length are eligible