我有一个大型的多行文本文件,将行内容读入bash变量,直到特定字符(终止符)

问题描述 投票:0回答:1

说我有一个包含以下内容的文件,没有格式,但是字段分隔符为“;”

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 unix
1个回答
0
投票

在bash上,可以使用read选项更改-d命令的定界符。

$ read -rd\; VAR < file
$ echo "$VAR"
This is  one field
on a line 
with some characters
© www.soinside.com 2019 - 2024. All rights reserved.