La solution déjà affichée d'utiliser awk ou sed est assez standard et aide au cas où quelque chose ne fonctionnerait pas correctement.Un script pur Bash Cuting qui ne fournit pas un travail efficace
comme pour:
StringStr="ValueA:ValueB,ValueC:ValueC" ;
echo ${StringStr} | gawk -F',' 'BEGIN{}{for(intx=1;intx<=NF;intx++){printf("%s\n",$(intx))}}END{}'
ne produisent le même résultat, mais un utilisateur restreint qui peut s'y connecter en son compte et ont moins option comme pas autorisés à utiliser awk ou gawk pour une raison spécifique ne doit produire quelque chose qui doit fonctionner à chaque fois.
Pour des raisons efficace je développer ma propre bibliothèque de fonctions Bash sur github.com et tomber sur une technique qui ne fonctionne pas comme on le suppose et ici un exemple de travail:
Cette technique utiliser le modèle préfixe « Supprimer correspondant Bash 'et' Supprimer le motif de suffixe correspondant '. Le but est d'obtenir une chaîne d'informations chaînées pour utiliser le plus simplement possible l'élément bash-shell pour extraire l'élément inséré.
Par la présente j'ai première déclaration pour obtenir une chaîne d'un format spécifique: Ex:
StringPattern="__VALUE1__:__VALUE2__,"
Le format suppose l'ajout de la chaîne, de nombreux modèles de type StringPattern. Le reste ',' sera utilisé pour diviser et séparer la chaîne en VALUE1: VALUE2.
comme StringStorage tiendra à plusieurs reprises, analysable StringPattern, voici 2 exemples: 1 - échantillon 1
StringPattern="VariableA:InformationA,"
StringStorage="${StringStorage}${StringPattern}" ;
2 - échantillon 2
StringPattern="VariableB:InformationB,"
StringStorage="${StringStorage}${StringPattern}" ;
A ce moment, StringStorage tenir correctement ces informations :
StringStorage="VariableA:InformationA,VariableB:InformationB,"
Maintenant avec StringStorage, le bas algorithme h fait d'un mélange de « supprimer le motif de préfixe correspondant » et « Supprimer le motif de suffixe correspondant » ne fonctionne pas pour ce cas:
### Description of IntCsvCount
### does remove all chosed Comma Separated value ',' from StringStorage
### and subtract from the original length the removed result from this
### subtraction. This produce IntCsvCount == 2
IntCsvCount=$(cstr=${StringStorage//,/} ; echo $((${#StringStorage} - ${#cstr}))) ;
### Description of
### Will be re Variable used to put the Extracted sequence.
bstr="" ;
### Description of for
### Received information from IntCsvCount it should count
### from 0 to Last element . This case it's ${IntCsvCount}-1 or 1 in
### my example.
for ((intx=0 ; intx <= ${IntCsvCount}-1 ; intx++)) ; do
### This extracting First Segment based on
### Remove matching suffix pattern ${parameter%word} where
### work is ${astr#*,} (Remove matching prefix pattern) of
### everything in $astr until find a ',' .
bstr=${astr%*${astr#*,}} ;
### Destroying the $bstr part in by starting the astr to position of
### the end of size equivalent of bstr size (${#bstr}), end position is
### equal to [ Highest-String size ] - [ Shortest-String size ]
astr=${astr:${#bstr}:$((${#astr} - ${#bstr}))} ;
echo -ne "Element: ${bstr}\n" ;
done
Cela devrait produire la réponse suivante.
Element: VariableA:InformationA,
Element: VariableB:InformationB,
Mettre cela en fonction ne demandera de changer le CSV par « : » et laisser extraire le « variableA » et « informationa ».
Le problème commence à l'aide d'une chaîne avec un non uniforme. Comme observé sur ce tableau, l'exemple d'une phrase et la coupe d'une partie devraient travailler sur une corde non-uniforme, mais ici un échantillon qui ne fonctionne pas.Et je n'ai plus d'un conseiller à la main d'utiliser gawk, sed, même coupé mais de cet algorithme, il ne fonctionne pas avec cet exemple:
astr="master|ZenityShellEval|Variable declaration|Added Zenity font support to allow choosing both font-name and size and parsing the zenition option, notice --font option require a space between font and size.|20170127|"
comming de
astr=$(zenity --width=640 --height=600 --forms --show-header --text="Commit Message" --add-entry="Branch name" --add-entry="function" --add-entry="section" --add-entry="commit Message" --add-calendar="Commit Date" --forms-date-format="%Y%m%d" --separator='|') ;
Je Faire respecter aussi la sortie doit ressembler à ce que StringPattern devrait ressembler: astr = "$ {astr} |" ;
Le même code, à l'exception de CSV (Comma Separated Value), est passé de ',' à '|'
IntCsvCount=$(cstr=${astr//|/} ; echo $((${#astr} - ${#cstr}))) ;
bstr="" ;
for ((intx=0 ; intx <= ${IntCsvCount}-1 ; intx++)) ; do
bstr=${astr%*${astr#*|}} ;
astr=${astr:${#bstr}:$((${#astr} - ${#bstr}))} ;
echo -ne "Element: ${bstr}\n" ;
done
Si cette sortie de temps générer la sortie suivante:
Element:master|ZenityShellEval|Variable declaration|Added Zenity font support to allow choosing both font-name and size and parsing the zenition option, notice --font option require a space between font and size.|20170127|
Element:
Element:
Element:
Y at-il une raison pour laquelle il ne devrait pas travailler chaque fois?
Ceci est assez difficile à suivre. Pouvez-vous le simplifier à votre entrée, la sortie réelle et attendue, et quel code vous avez essayé d'obtenir ce résultat. – 123