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 it
mails2=($IN) 我在循环中打印它时只得到第一个字符串,没有括号围绕$IN它起作用。 您可以设置内部字段分隔符(IFS)变量,然后将其解析为数组。当在命令中发生这种情况时,对IFS的分配仅发生在该单个命令的环境中(要read)。然后它根据IFS变量值将输入解析为一个数组,然后我们可以迭代它。 IFS=';'read-ra ADDR ...
shell 在bash中将字符串拆分为数组简介 在这个函数的底部,你会发现一个函数,它可以将string转换为一个array,语法如下:使用
progname=${0##*/} ## Get the name of the script without its path ## Default values verbose=0 filename= ## List of options the program will accept; ## those options that take arguments are followed by a colon optstring=f:v ## The loop calls getopts until there are no more options...
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() { # Usage: split "string" "delimiter" IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}" printf '%s\n' "${arr[@]}" }示例用法:$ split "apples,oranges,pears,grapes" "," apples oranges pears grapes $ split "1, 2, 3, 4, 5" ", " 1 2 3 4 5 # 多...
In this example, we used awk to split a string into an array. Thesplitfunction in awk divides the string into an arrayausing space as the delimiter. We then printed the second element of the array, resulting in the output ‘Array’. ...
The output of this script is: 12-3 请注意, even hyphens are placed as characters; therefore, this method cannot split the string on any delimiter. Instead, it breaks the string into an array of characters. There is one exception to this approach. If you use spaces between characters, the...
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[@]}" }...
A double-quoted string preceded by a dollar sign (‘$’) will cause the string to be translated according to the current locale. If the current locale isCorPOSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted. ...