GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Dividere la corda, estrarre i caratteri e rimettere insieme?

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

  1. Copia lo script in un file vuoto, salvalo come rearrange.py
  2. 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.


Ubuntu
  1. Ubuntu:come configurare le autorizzazioni per consentire a Gedit, Apache e An Ide di giocare insieme?

  2. Come disconnettersi e quindi accedere nuovamente a Ubuntu Software Center?

  3. Riporta Backspace a Nautilus (3.18+) una volta per tutte!?

  4. Come eseguire la ricerca inversa avanti e indietro?

  5. Ottenere sudo e nohup per lavorare insieme

Come eseguire il backup e il ripristino dei siti in Plesk

Come eseguire il backup e il ripristino dei database in Plesk

Come utilizzare il comando SED per trovare e sostituire la stringa nei file

Come convertire PDF in immagini PNG e viceversa in Linux

Costruire un comando inserendo una stringa in un Tty?

Come utilizzare Sed per trovare e sostituire una stringa in un file