ho ottenuto un 2,7 script python che risolve il problema risolvendo il problema più generale di crollare diverse linee che cambiano solo da un numero di sequenza
import re
def do_compress(old_ints, ints):
"""
whether the ints of the current entry is the continuation of the previous
entry
returns a list of the indexes to compress, or [] or False when the current
line is not part of an indexed sequence
"""
return len(old_ints) == len(ints) and \
[i for o, n, i in zip(old_ints, ints, xrange(len(ints))) if n - o == 1]
def basic_format(file_start, file_stop):
return "[seq]{} .. {}".format(file_start, file_stop)
def compress(files, do_compress=do_compress, seq_format=basic_format):
p = None
old_ints =()
old_indexes =()
seq_and_files_list = []
# list of file names or dictionaries that represent sequences:
# {start, stop, start_f, stop_f}
for f in files:
ints =()
indexes =()
m = p is not None and p.match(f) # False, None, or a valid match
if m:
ints = [int(x) for x in m.groups()]
indexes = do_compress(old_ints, ints)
# state variations
if not indexes: # end of sequence or no current sequence
p = re.compile(\
'(\d+)'.join(re.escape(x) for x in re.split('\d+',f)) + '$')
m = p.match(f)
old_ints = [int(x) for x in m.groups()]
old_indexes =()
seq_and_files_list.append(f)
elif indexes == old_indexes: # the sequence continues
seq_and_files_list[-1]['stop'] = old_ints = ints
seq_and_files_list[-1]['stop_f'] = f
old_indexes = indexes
elif old_indexes ==(): # sequence started on previous filename
start_f = seq_and_files_list.pop()
s = {'start': old_ints, 'stop': ints, \
'start_f': start_f, 'stop_f': f}
seq_and_files_list.append(s)
old_ints = ints
old_indexes = indexes
else: # end of sequence, but still matches previous pattern
old_ints = ints
old_indexes =()
seq_and_files_list.append(f)
return [ isinstance(f, dict) and seq_format(f['start_f'], f['stop_f']) or f
for f in seq_and_files_list ]
if __name__ == "__main__":
import sys
if len(sys.argv) == 1:
import os
lst = sorted(os.listdir('.'))
elif sys.argv[1] in ("-h", "--help"):
print """USAGE: {} [FILE ...]
compress the listing of the current directory, or the content of the files by
collapsing identical lines, except for a sequence number
"""
sys.exit(0)
else:
import string
lst = [string.rstrip(l, '\r\n') for f in sys.argv[1:] for l in open(f)])
for x in compress(lst):
print x
cioè sui tuoi dati:
bernard $ ./ls_sequence_compression.py given_data
[seq]filename_v003_0001.geo .. filename_v003_0007.geo
[seq]filename_v003_0032.geo .. filename_v003_0036.geo
[seq]testxxtest.0057.exr .. testxxtest.0063.exr
Si basa sulle differenze tra gli interi presenti in due righe consecutive che corrispondono al testo non numerico. Ciò consente di gestire input non uniformi, sulle modifiche del campo utilizzato come base per la sequenza ...
Ecco un esempio di ingresso:
01 - test8.txt
01 - test9.txt
01 - test10.txt
02 - test11.txt
02 - test12.txt
03 - test13.txt
04 - test13.txt
05 - test13.txt
06
07
08
09
10
che dà:
[seq]01 - test8.txt .. 01 - test10.txt
[seq]02 - test11.txt .. 02 - test12.txt
[seq]03 - test13.txt .. 05 - test13.txt
[seq]06 .. 10
Ogni commento è il benvenuto!
Hah ... I nearby forgot: senza argomenti, questo script restituisce il contenuto collassato della directory corrente.
Bella domanda! Vuoi anche la colorazione? : p E quanto lontano vuoi andare con questo? Voglio dire, cosa vuoi fare con qualcosa come 'file1-001-1.txt-0',' file1-001-1.txt-1', ..., 'file1-001-2.txt-0 ',' file1-001-2.txt-1', ..., 'file1-002-1.txt-0',' file1-002-1.txt-1', ... Sarà più difficile per riconoscere o rappresentare rispetto alle sequenze che hai dato a –
Per quanto sia bello che funzioni sempre, ho solo bisogno che funzioni nella situazione che ho postato, * ####. estensione. Non sono sicuro se questo è qualcosa che dovrei fare in Python o Straight Bash. Conosco solo un po 'di battuta e il tipo di volevo usare questo come punto di partenza per imparare meglio però. – phimath
un altro problema però è che non tutte le estensioni che uso sono 3 caratteri, e alcuni di essi hanno. Sono in essi, come 'bgeo.gz'. Potrei fare una lista di tutte le estensioni che uso, ma mi piacerebbe trovare una soluzione più elegante, come ad esempio prendere le ultime 4 cifre numeriche. – phimath