说我有一个包含以下内容的文件,没有格式,但是字段分隔符为“;”
This is one field
on a line
with some characters;
This is one field
on a line with some characters;
This is one field on a line with some characters;
将字段分配给这样的变量。VAR =“这是一个字段在一条线上带有一些字符“
尝试以下代码
IFS=';'
while read -r line
do
VAR="$line"
done < "$file"
但是VAR仅包含“这是一个字段”它没有使用IFS =;并且只选择每行换行。
在bash上,可以使用read
选项更改-d
命令的定界符。
$ read -rd\; VAR < file
$ echo "$VAR"
This is one field
on a line
with some characters