Oracle Magazine

Risorse


Designed by:
SiteGround web hosting Joomla Templates
Split a string without 'cut' or 'awk' PDF Stampa E-mail
Scritto da Andrea Salzano   
Domenica 14 Febbraio 2010 16:55
=================
Versione Italiana
=================

L'articolo originale è in [1]: viene descritto come ottenere lo split di una stringa in bash.

original='string1;string2;string3'
part1=${original%%;*}; rest=${original#*;}
part2=${rest%%;*}; rest=${rest#*;}
part3=${rest%%;*};

Nell'ottica di chiarire il concetto di "Parameter Expantions" ho costriuto un esempio. In generale, "Parameter Espantions" è:

${var#PATTERN}    Rimuovi la corrispondenza più piccola
                  a partire dall'inizio
${var##PATTERN}  
Rimuovi la corrispondenza più grande
                  a partire dall'inizio

${var%PATTERN}    Rimuovi la corrispondenza più piccola
                  a partire dalla fine

${var%%PATTERN}   Rimuovi la corrispondenza più grande
                  a partire dalla fine


Così:
  1. Definisco una variabile "var" come "this;is;a;very;long;longer;string"
  2. La corrispondenza (PATTERN) che cerco è "*;" oppure ";*"
Tutti e 4 i casi sono illusrtari di seguito.

=========
${var#*;}
=========
Significato:
a partire dall'inizio, elimina la stringa più breve che corrisponde a "<qualsiasicosa>;"

this;is;a;very;long;longer;string
    ^
    |
----

risultato
: is;a;very;long;longer;string

==========
${var##*;}
==========
Significato: 
a partire dall'inizio, elimina la stringa più lunga che corrisponde a "<qualsiasicosa>;"

this;is;a;very;long;longer;string
                          ^
                          |
--------------------------


risultato: string

=========
${var%;*}
=========
Significato: 
a partire dalla fine, elimina la stringa più breve che corrisponde a ";<qualsiasicosa>"

this;is;a;very;long;longer;string
                          ^
                          |
--------------------------


risultato: this;is;a;very;long;longer

==========
${var%%;*}
==========
Significato: 
a partire dalla fine, elimina la stringa più lunga che corrisponde a ";<qualsiasicosa>"

this;is;a;very;long;longer;string
    ^
    |
----


risultato: this


Riderimenti:
[1] http://antonolsen.com/2006/04/10/bash-split-a-string-without-cut-or-awk/
[2] http://linuxgazette.net/issue18/bash.html


===============
English Version
===============

The original thread is in [1]: you can find how to split a string in bash.

original='string1;string2;string3'
part1=${original%%;*}; rest=${original#*;}
part2=${rest%%;*}; rest=${rest#*;}
part3=${rest%%;*};

In order to explain me cleary how to Parameter Expantions works in bash I make some example. Generally speak Parameter Espantions is:

${var#PATTERN}    Remove the shortest match from the beginning
${var##PATTERN}   Remove the longest match from the beginning
${var%PATTERN}    Remove the shortest match from the end
${var%%PATTERN}   Remove the longest match from the end

So
  1. I define a variable "var" like "this;is;a;very;long;longer;string"
  2. The pattern matching is "*;" or ";*"
All 4 case are explained below.

=========
${var#*;}
=========
Means:
from the begining, remove the shortest string that match "<anything>;"

this;is;a;very;long;longer;string
    ^
    |
----

output
: is;a;very;long;longer;string

==========
${var##*;}
==========
Means: from the begining, remove the longest string that match "<anything>;"

this;is;a;very;long;longer;string
                          ^
                          |
--------------------------


output: string


=========
${var%;*}
=========
Means:
from the end, remove the shortest string that match ";<anything>"

this;is;a;very;long;longer;string
                          ^
                          |
--------------------------


output: this;is;a;very;long;longer


==========
${var%%;*}
==========
Means: from the end, remove the longest string that match ";<anything>"

this;is;a;very;long;longer;string
    ^
    |
----


output: this


Reference:
[1] http://antonolsen.com/2006/04/10/bash-split-a-string-without-cut-or-awk/
[2] http://linuxgazette.net/issue18/bash.html

Ultimo aggiornamento Mercoledì 17 Febbraio 2010 08:55
 

Login



DOCman Category


Non ci sono documenti

Automatic Google Translator V2