Ceci est un script très courant:
#!/bin/bash teststr="col1 col2" var1=`echo ${teststr} | awk '{print $1}'` var2=`echo ${teststr} | awk '{print $2}'` echo var1=${var1} echo var2=${var2}
4 Réponses :
Cela pourrait fonctionner pour vous:
var=(col0 col1 col2) echo "${var[1]}" col1
Merci de me rappeler Bash Arries. C'est la solution. On peut aussi écrire comme ceci: var = ( echo $ {blabla} | awk -f "_" '{print 1 $ "" 3} $' code>) ...
bash a un support de réseau, nous devons simplement fournir des valeurs de manière dynamique :) Documentation de tableau Bash: http://tldp.org/ldp/abs/html/arrays.html p> p>
Vous pouvez également utiliser coquille définir code> intégré pour placer des espaces blanche séparés (ou plus précisément,
ifs code> séparé) dans les variables
1 $ code>,
2 $ code> et ainsi de suite:
#!/bin/bash
teststr="col1 col2"
set -- $teststr
echo "$1" # col1
echo "$2" # col2
Oui, vous pouvez, mais la meilleure pratique consiste à utiliser le moyen awk pour passer des variables à AWK forte>. Exemple à l'aide de variables de script shell forts> p> Il est possible d'utiliser des variables de script et d'environnement à la Même heure forte> p> #!/bin/bash
# set some variables
# set path to the config file to edit
CONFIG_FILE=/etc/netplan/01-netcfg.yaml
# find the line number of the line to change using awk and assign it to a variable
DHCP_LINE=$(awk '/dhcp4: yes/{print FNR}' $CONFIG_FILE)
# get the number of spaces used for identation using awk and assign it to a variable
SPACES=$(awk -v awkDHCP_LINE="$DHCP_LINE" 'FNR==awkDHCP_LINE {print match($0,/[^ ]|$/)-1}' $CONFIG_FILE)
# find DHCP setting and turn it off if needed
awk -v awkDHCP_LINE="$DHCP_LINE" -v awkSPACES="$SPACES" 'FNR==awkDHCP_LINE {sub("dhcp4: yes", "dhcp4: no")}' $CONFIG_FILE
Lire -R A B << (AWK '{IMPRIMER 1 $, 2 $}' <<< "Hello World") Code>