“scission de bash” Réponses codées

Comment diviser une chaîne en bash

string="you got a friend in me"
IFS=' ' read -ra split <<< "$string"
echo "${split[*]}"
# Output: you got a friend in me
echo "${split[3]}"
# Output: friend
AttractivePenguin

scission de bash

# Basic syntax:
cut -d "delimeter" -f split_number your_file
# Where:
#	- -d specifies the delimeter to split by
#	- -f specifies which element to return after splitting
# Note, if running this in a bash script, use syntax like:
"$(cut -d ',' -f split_number <<<$variable_to_split)"

# Note, there are lots of ways of doing this, e.g. with awk:
awk -F delimiter '{print $split_number}' your_file
# Where:
#	- -F specifies the field delimiter

# Note, awk also has a split function which has this syntax:
awk '{split($column, a, "delimiter"); action_after_split }' your_file
# Where:
#	- the column is the column to be split (awk uses whitespace to determine
#		columns by default)
#	- a is an array variable which will store the contents of the split
#	- delimiter is the delimiter by which to split the column

# Example usage:
# Say you have a file with this line:
my	file	with	fields_to_split
# You can print "to" with:
awk '{split($4, a, "_"); print a[2] }' your_file
--> to
Charles-Alexandre Roy

Faire part de la chaîne divisée en variables

# separator is space
VAR="inforge srl bergamo"
read -r ONE TWO THREE <<< "${VAR}"
echo ${ONE}
echo ${TWO}
echo ${THREE}

# separator is comma
VAR="inforge,srl,bergamo"
IFS="," read -r ONE TWO THREE <<< "${VAR}"
echo "${ONE} ${TWO} ${THREE}"
sambo

Variable de division bash par délimiteur

IFS='|' read -r -a arrayName <<< "$variable"
C0W

Réponses similaires à “scission de bash”

Questions similaires à “scission de bash”

Plus de réponses similaires à “scission de bash” dans Shell/Bash

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code