Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Finding longest common substring among filenames

Shell Programming and Scripting


Tags
file, shell help, string manipulation

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-12-2008
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 27 July 2015, 12:41 PM EDT
Location: Toronto, Canada
Posts: 2,896
Thanks: 0
Thanked 131 Times in 116 Posts

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:19 PM.