Ho stringhe sotto forma di wva/sia/e1
, bct/e2
, sv/de/e11
. È sempre <Part1>/e<NUM>
o <Part1>/<Part2>/e<NUM>
. Quello che voglio è accorciare le stringhe mantenendo le prime lettere delle parti e abbandonando le barre ed e:
wva/sia/e1 > ws1
bct/e2 > b2
sv/de/e11 > sd11
Come posso farlo all'interno di uno script sh?
Modifica:la stringa rappresenta il nome di un lavoro:
[...]
job_name=$1 # e.g. 'wva/sia/e1'
job_name=cut_name(job_name) # e.g. 'ws1'
[...]
Risposta accettata:
Sotto forma di script come quello che chiedi:
#!/usr/bin/env python3
import sys
# read the input, split by /
st = sys.argv[1].split("/")
# get the first char of all sections *but* the last one
# add the last *from* the first character
print("".join([s[0] for s in st][:-1])+st[-1][1:])
Nota che questo funziona per qualsiasi lunghezza, ad esempio:
wva/sia/bct/wva/sia/e1
diventerà
wsbws1
purché l'ultima sezione termini con /e<num>
Da usare
- Copia lo script in un file vuoto, salvalo come
rearrange.py
-
Eseguilo con la stringa come argomento, ad esempio:
python3 /path/to/rearrange.py wva/sia/e1 > ws1
Spiegazione
La sceneggiatura praticamente si spiega da sola, ma è anche commentata.