Finding longest common substring among filenames | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Finding longest common substring among filenames

Shell Programming and Scripting


Tags
file, shell help, string manipulation

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-12-2008
cmcnorgan cmcnorgan is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 14 February 2012, 11:36 AM EST
Location: London, Ontario, Canada
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Finding longest common substring among filenames

I will be performing a task on several directories, each containing a large number of files (2500+) that follow a regular naming convention:

YYYY_MM_DD_XX.foo_bar.A.B.some_different_stuff.EXT

What I would like to do is automatically discover the part of the filenames that are common to all 2500 files, so that a script could use that as a base name. In practice, this will end up being "YYYY_MM_DD_XX.foo_bar."

I figured out as far as I'll have to use ls to get all the filenames, but there's no command that I know of that will find the largest substring that exists among a large number of strings. I thought perhaps there would be some sed guru out there that would find this problem trivial. You sed experts always blow my mind.
Sponsored Links
    #2  
Old 12-12-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts

Code:
set -- *
files=$#
name=$1
while [ -n "$name" ]
do
  set -- $name*
  [ $# -eq $files ] && break
  name=${name%?}
done
echo $name

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
finding common numbers (contents) across 2 or 3 files Lucky Ali Shell Programming and Scripting 4 05-27-2011 02:08 PM
Finding longest line in a Record SEinT Shell Programming and Scripting 5 04-19-2011 02:17 AM
Finding Authors in Common Across Dozens of Lists Peggy White Shell Programming and Scripting 5 03-30-2009 09:45 AM
Finding the most common entry in a column Donkey25 Shell Programming and Scripting 12 11-22-2007 11:16 AM
finding the last substring... cutelucks Shell Programming and Scripting 7 11-04-2006 05:48 AM



All times are GMT -4. The time now is 09:51 PM.