/bin/bash## Script to split a string based on the delimitermy_string="Ubuntu;Linux Mint;Debian;Arch;Fedora"IFS=';'read-ra my_array <<<"$my_string"#Print the split stringforiin"${my_array[@]}"doecho$idone 拆分字符串的部分如下: 登录后复制IFS=';'read-ra my_array <<<"$my_string...
https://linuxhandbook.com/bash-split-string/ #!/bin/bash # # Script to split a string based on the delimiter my_string="One;Two;Three" my_array=($(echo $my_string | tr ";" "\n")) #Print the split string for i in "${my_array[@]}" do echo $i done Output One Two Three ...
/bin/bash## Script to split a string based on the delimitermy_string="Ubuntu;Linux Mint;Debian;Arch;Fedora"IFS=';'read-ramy_array<<<"$my_string"#Print the split stringfor i in "${my_array[@]}"doecho $idone 1. 2. 3. 拆分字符串的部分如下: 复制 IFS=';'read-ramy_array<<<"$...
split() { local string="$1" local delimiter="$2" if [ -n "$string" ]; then local part while read -d "$delimiter" part; do echo $part done <<< "$string" echo $part fi } Run Code Online (Sandbox Code Playgroud) 例如,命令 $ split 'a;b;c' ';' Run Code Online (Sandbox...
Method 1: Split string using read command in Bash Here’s my sample script for splitting the string using read command: #!/bin/bash # # Script to split a string based on the delimiter my_string="Ubuntu;Linux Mint;Debian;Arch;Fedora" IFS=';' read -ra my_array <<< "$my_string" #...
This example will adapt this command to split text at a given delimiter. For the full user manual of thecutcommand, click here. The Code Below is anexample Bash scriptwhich takes the stringsplitMeand returns items based on their position in the string split at the commas (,): ...
我在循环中打印它时只得到第一个字符串,没有括号围绕$IN它起作用。 答案 您可以设置内部字段分隔符(IFS)变量,然后将其解析为数组。当在命令中发生这种情况时,对IFS的分配仅发生在该单个命令的环境中(要read)。然后它根据IFS变量值将输入解析为一个数组,然后我们可以迭代它。
Use regex on a stringThe result of bash's regex matching can be used to replace sed for a large number of use-cases.CAVEAT: This is one of the few platform dependent bash features. bash will use whatever regex engine is installed on the user's system. Stick to POSIX regex features if...
Trim all white-space from string and truncate spaces Use regex on a string Split a string on a delimiter Change a string to lowercase Change a string to uppercase Trim quotes from a string Strip all instances of pattern from string Strip first occurrence of pattern from string Strip pattern ...
string to name value pairs array. function toNameValuePairsArray(){ #store args string to local fullNameValPairsString="$1" #first split the full name-value pair parameters(via comma delimiter). IFS=',' #Read the split words into an array based on space delimiter read -a nameValPairsArr...