/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<<<"$...
/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...
/bin/bash#Example for bash split string without $IFSread-p"Enter any string separated by colon(:) "str#reading string valuereadarray-d:-t strarr<<<"$str"#split a string based on the delimiter ':'printf"\n"#Print each value of Array with the help of loopfor((n=0;n<${#strarr[...
split1.sh #!/bin/bash #Define the string value text="Welcome to LinuxHint" # Set space as the delimiter IFS=' ' #Read the split words into an array based on space delimiter read -a strarr <<< "$text" #Count the total words echo "There are ${#strarr[*]} words in the text....
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...
Method 1: Split string using read command in Bash Here’s my sample script for splitting the stringusing 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" #Pr...
Split a string on a delimiterCAVEAT: Requires bash 4+This is an alternative to cut, awk and other tools.Example Function:split() { # Usage: split "string" "delimiter" IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}" printf '%s\n' "${arr[@]}" }...
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 (,): ...
Here, I have taken the stringi,am,one,line,from,a,csv,fileas the one I want to split. The variableIFS, is a special bash variable. It stands for Internal Field Separator. This variable holds what you would otherwise call adelimiter. Since I want to cause a split where a comma occurs...
我在循环中打印它时只得到第一个字符串,没有括号围绕$IN它起作用。 您可以设置内部字段分隔符(IFS)变量,然后将其解析为数组。当在命令中发生这种情况时,对IFS的分配仅发生在该单个命令的环境中(要read)。然后它根据IFS变量值将输入解析为一个数组,然后我们可以迭代它。